繁体   English   中英

为什么在Mongoose模式中存在未定义的类型?

[英]Why is there an undefined type in my Mongoose Schema?

每次我尝试使用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 ,因为您是根据promoSchema创建模型,而不是根据Schema创建模式! 猫鼬会尝试根据您的参数创建一个模式,从而引发错误。 采用:

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

Mongoose文档中了解有关模型的更多信息。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM