Javascript 为什么在我的Mongoose模式中有一个未定义的类型?
每次我尝试使用命令Javascript 为什么在我的Mongoose模式中有一个未定义的类型?,javascript,node.js,mongoose,Javascript,Node.js,Mongoose,每次我尝试使用命令npm start从命令行运行服务器时,我都会收到以下错误消息: throw new TypeError('Undefined type `' + name + '` at `' + path + ^ TypeError: Undefined type `P` at `0` 以下是据报道导致问题的模块: var mongoose = require('mongoose'); var Currency = require('mongoose-currency').l
npm start
从命令行运行服务器时,我都会收到以下错误消息:
throw new TypeError('Undefined type `' + name + '` at `' + path +
^
TypeError: Undefined type `P` at `0`
以下是据报道导致问题的模块:
var mongoose = require('mongoose');
var Currency = require('mongoose-currency').loadType(mongoose);
var Schema = mongoose.Schema;
var promoSchema = Schema({
name: {
type: String,
required: true,
unique: true
},
image: {
type: String,
required: true,
unique: true
},
label: {
type: String,
required: false,
default: ""
},
price: {
type: Currency,
required: true
},
description: {
type: String,
required: true
}
},
{
timestamps: true
});
var Promotions = Schema('Promotion', promoSchema);
module.exports = Promotions;
为什么会抛出错误?您将模式和模型混为一谈。这行不正确:
var Promotions = Schema('Promotion', promoSchema);
Schema
应该是mongoose.model
,因为您是根据promotschema
创建模型,而不是根据您的Schema创建模式!Mongoose试图用您的参数创建一个模式,因此抛出错误。使用:
var Promotions = mongoose.model('Promotion', promoSchema)
了解更多有关模型的信息