为什么我的Mongoose Schema中存在未定义的类型?

时间:2017-05-30 17:10:51

标签: 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').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;

为什么会抛出错误?

1 个答案:

答案 0 :(得分:0)

你混淆了架构和模型。这一行不正确:

var Promotions = Schema('Promotion', promoSchema);

Schema应为mongoose.model,因为您正在使用promoSchema创建模型,而不是模式中的模式! Mongoose尝试从您的参数中创建一个Schema,从而抛出错误。使用:

var Promotions = mongoose.model('Promotion', promoSchema)

Mongoose documentation了解有关模特的详情。