【问题标题】:Mongoose errors on populate填充上的猫鼬错误
【发布时间】:2018-11-05 19:34:54
【问题描述】:

我有两个相互关联的集合。一个会话可以包含多个学生,我想通过使用 populate() 来检索这些学生。这些是架构的:

const studentSchema = new Schema({
    first_name: String,
    last_name: String
}) 

const sessionSchema = new Schema({
    course_code: String,
    students: [{ type: Schema.Types.ObjectId, ref: 'Student' }]
})

const Session = mongoose.model('sessions', sessionSchema)
const Student = mongoose.model('students', studentSchema)

每当我在会话或学生上使用 findOne() 时,它都会提供所需的输出。但是,当我像这样使用 populate() 时,它给了我一个错误:

Session
    .findOne({'course_code': '5072NEAN6Y'})
    .populate("students")
    .exec(function (err, ps){
        if(err){
            console.log(err);
            return;
         }
         console.log("succes");
});

错误: MissingSchemaError:尚未为模型“学生”注册架构。

谁能告诉我我做错了什么?

【问题讨论】:

  • 您是否在使用架构的文件中添加了 require() ?
  • 模式与 populate() 在同一个文件中,所以我不需要模型,对吧?

标签: mongoose mongoose-populate


【解决方案1】:

显然改变了 populate() 的参数使其工作:

.populate({path: 'students', model: Student})

【讨论】:

    猜你喜欢
    • 2017-04-04
    • 1970-01-01
    • 2017-04-18
    • 2015-07-13
    • 2016-10-10
    • 2019-09-16
    • 2018-10-27
    • 2015-07-13
    相关资源
    最近更新 更多