Populate self-referencing model

12 views
Skip to first unread message

Darko Milutinovic

unread,
Jun 9, 2015, 10:09:13 AM6/9/15
to mongoo...@googlegroups.com
I have the following case:

var testSchema = new mongoose.Schema(
    {
      name:      { type: String, required: true },
      title:         { type: String },
      subArray: [ testSchema ]
    }
  );

var testModel = mongoose.model('Test', testSchema);

When I do the following:

testModel.findOne({ name: 'test-name' }).exec(function(err, doc) {

          doc.populate({ path: 'subArray', model: 'testModel' }, function(err, d) { });

});

doc is found but after the populate function executes the subArray is not populated.
It shows that there is one element in subArray (correctly) but the actual element itself is undefined.

How do I populate subArray?
What am i doing wrong?

mongoose version I'm using is 3.8.22.
Reply all
Reply to author
Forward
0 new messages