简体   繁体   中英

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:

 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! 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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