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 ?

Answers


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.


Need Your Help

MySQL - Fulltext Index Search Issue

mysql full-text-search

Two rows in the my database have the following data: