每次我尝试使用命令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;
为什么会抛出错误?
答案 0 :(得分:0)
你混淆了架构和模型。这一行不正确:
var Promotions = Schema('Promotion', promoSchema);
Schema
应为mongoose.model
,因为您正在使用promoSchema
创建模型,而不是模式中的模式! Mongoose尝试从您的参数中创建一个Schema,从而抛出错误。使用:
var Promotions = mongoose.model('Promotion', promoSchema)
在Mongoose documentation了解有关模特的详情。