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

前端 未结 16 1110
别跟我提以往
别跟我提以往 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:25

    I've also experienced this error with ES6/Typescript. Even I imported the model, the error still persisted. According to docs here

    MongooseError.MissingSchemaError

    Thrown when you try to access a model that has not been registered yet

        import { model, Schema } from 'mongoose';
        import Company from './CompanyModel';
    
        const ProjectSchema = new Schema({
            company: { type: Schema.Types.ObjectId, ref: "Company" }
        });
    
        export default model('Project', ProjectSchema);
    

    The tips was just to make sure to use the model explicitly, so changing ref:"Company" into ref:Company.modelName seemed fixed it.

    I hope that helps some of you

提交回复
热议问题