MissingSchemaError: Schema hasn't been registered for model “User”

前端 未结 16 1068
别跟我提以往
别跟我提以往 2020-12-02 16:56

In my models/user.js file:

var mongoose = require(\'mongoose\');
var Schema = mongoose.Schema;

var userSchema = new Schema({
    (define schema         


        
16条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-02 17:21

    In my case, it happened because of capital/small letter confusion. User model had this:

    const userSchema = new Schema({
    // ...
    });
    module.exports = mongoose.model('User', userSchema);
    

    And Product model had a reference to User model but small case:

    const productSchema = new Schema({
    // ...
      userId: {
        type: Schema.Types.ObjectId,
        ref: 'user', // This should exactly match the name of User model above!!!
        required: true
      }
    });
    

提交回复
热议问题