【问题标题】:When call populate TypeError: Cannot read property 'ref' of undefined当调用填充类型错误:无法读取未定义的属性'ref'
【发布时间】:2014-01-31 03:06:14
【问题描述】:

我正在尝试按名称添加负载学生并填充所有数据。 我的学生架构看起来像

// Exam Schema
var ExamSchema = new Schema({
    name: String,
    mark: String,
    teacher: String,
    activated: Date
});

// Student Schema
var StudentSchema = new Schema({
    name: String,
    description: String,
    exams: [ExamSchema],
    activated: Date
});

我添加到 StudentSchema 静态函数

  loadByName: function (name, cb) {
    this.findOne({ name : name})
      .populate('exams')
      .exec(cb)
  },

ad 当我打电话时我得到错误

TypeError: Cannot read property 'ref' of undefined
    at model.populate [as _populate] 

当我评论填充行时,我得到了学生,但考试列表为空。 如何按姓名查找一名学生并填充该学生的所有考试?

【问题讨论】:

    标签: node.js mongodb mongoose


    【解决方案1】:

    您遇到的问题是populate 是针对您通过引用链接到另一个文档的情况,而架构中的 exams 字段是一个子文档数组。它们应该自动包含完整的信息。

    有关详细信息,请参阅有关人口 (http://mongoosejs.com/docs/populate.html) 和子文档 (http://mongoosejs.com/docs/subdocs.html) 的文档页面。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-21
      • 1970-01-01
      • 2018-04-29
      • 1970-01-01
      • 1970-01-01
      • 2021-12-03
      • 2021-11-22
      • 2017-08-12
      相关资源
      最近更新 更多