简体   繁体   中英

When call populate TypeError: Cannot read property 'ref' of undefined

I am trying to add load Student by name and populate all data. My Student Schema looks like

// 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
});

I added to StudentSchema statics function

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

ad When I call I get error

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

when I commented populate line I get Student but exams list was empty. How to find One student by name and populate all exams for that student ?

The issue you're having is that populate is for when you've linked to another document by reference, where as the exams field in your schema is an array of subdocuments. They should contain the full information automatically.

See the documentation pages on population ( http://mongoosejs.com/docs/populate.html ) and subdocuments ( http://mongoosejs.com/docs/subdocs.html ) for more information.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM