Mongoose Schema hasn't been registered for model

前端 未结 17 856
猫巷女王i
猫巷女王i 2020-11-27 15:00

I\'m learning the mean stack and when I try to start the server using

npm start

I get an exception saying that:

schema has         


        
相关标签:
17条回答
  • 2020-11-27 15:31

    The issue is with the refs, always make sure to refer the refs to whatever name your are exporting from the models.

    // Model

    const Task = mongoose.model('**Tasks**', taskSchema);
    

    //Refs

    userSchema.virtual('tasks', {
    ref: '**Tasks**',
    localField: '_id', // field in current model
    foreignField: 'owner' // corresponding field in other model
    

    });

    0 讨论(0)
  • 2020-11-27 15:32

    You're not giving the model any value

    In my case, I was using a model for which I didn't updated when making the MongoDB connection.

    So, I had something like

    const Device = require('../../models/device')
    
    // make use of Device
    

    with this connection

    conn = await mongo.createConnection(conn,
          [JobApplication, Job, User])
    

    Fix

    You have to add the model to the conn when initiating the connection

    conn = await mongo.createConnection(conn,
          [JobApplication, Job, User, Device])
    

    Note that I added Device to the connection.

    0 讨论(0)
  • 2020-11-27 15:33

    This error also pops up when we create wrong references (ref) between mongoose models.

    In my case I was referring to the file name instead of model name.

    eg:

    const userModel = mongoose.model("user", userSchema);
    

    We should refer to 'user' (model name) instead of 'User' (file name);

    0 讨论(0)
  • 2020-11-27 15:33

    You should also check that you don't have dirty data in your database. I ended up with a document containing the lowercased version of the referenced model (user instead of User). This causes the error and is incredibly hard to track down.

    Easy to fix with a quick mongo query:

    db.model.updateMany({ approvedByKind: 'user' }, { $set: { approvedByKind: 'User' } })
    
    0 讨论(0)
  • 2020-11-27 15:39

    Elaborating on Rafael Grilli's answer above,

    Correct:

    var HouseSchema = new mongoose.Schema({
      date: {type: Date, default:Date.now},
      floorplan: String,
      name:String,
      house_id:String,
      addressLine1:String,
      addressLine2:String,
      city:String,
      postCode:String,
      _locks:[{type: Schema.Types.ObjectId, ref: 'xxx'}] //ref here refers to the first parameter passed into mongoose.model()
    });
    var House = mongoose.model('xxx', HouseSchema, 'houseschemas');
    
    0 讨论(0)
  • 2020-11-27 15:41
    .\nodeapp\node_modules\mongoose\lib\index.js:452
          throw new mongoose.Error.MissingSchemaError(name);
          ^
    MissingSchemaError: Schema hasn't been registered for model "users".
    Use mongoose.model(name, schema)
        at new MissingSchemaError
    

    I got this error resolved when use setTimeout on server.js

    mongoose.connect(env.get('mongodb.uri'), { useNewUrlParser: true })
      .then(() => logger.info("MongoDB successfully connected"))
      .catch(err => logger.error(err));
    app.use(passport.initialize());
    setTimeout(function() {
      require("./src/utils/passport")(passport);
    }, 3000);
    
    0 讨论(0)
提交回复
热议问题