简体   繁体   English

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

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

Every time I try to run my server from the command line with the command npm start I keep getting this error message: 每次我尝试使用npm start命令从命令行运行服务器时,都会不断收到以下错误消息:

 throw new TypeError('Undefined type `' + name + '` at `' + path +
    ^

TypeError: Undefined type `P` at `0`

Here is my module that is reportedly causing the problem: 这是我的模块,据说导致该问题:

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;

Why is the error being thrown? 为什么会引发错误?

You're mixing up Schemas and Models. 您正在混淆模式和模型。 This line is incorrect: 这行是不正确的:

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

Schema should be mongoose.model , because you're creating a model out of your promoSchema , not a Schema out of your Schema! Schema应该是mongoose.model ,因为您是根据promoSchema创建模型,而不是根据Schema创建模式! Mongoose tries to create a Schema out of your arguments and thus throws the error. 猫鼬会尝试根据您的参数创建一个模式,从而引发错误。 Use: 采用:

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

Learn more about Models at the Mongoose documentation . Mongoose文档中了解有关模型的更多信息。

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

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