Reputation: 8602
First = new mongoose.Schema({
name: String,
second: {type: Schema.Types.ObjectId, ref: 'Second'},
});
Second = new mongoose.Schema({
name: String,
third: {type: Schema.Types.ObjectId, ref: 'Third'},
});
Third = new mongoose.Schema({
name: String
});
First.find({}).populate({
path: 'Second',
populate: { path: 'Third'}
}).exec(function(err, result) {
console.log(result)
})
First populate is ok, but Third is always null
. Meaning I got some thing like this:
{
name: 1,
second: {
name: 2,
third: null
}}
Upvotes: 2
Views: 547
Reputation: 87
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
mongoose.connect('mongodb://localhost/test');
var FirstSchema = new mongoose.Schema({
name: String,
second: {type: Schema.Types.ObjectId, ref: 'Second'},
});
var SecondSchema = new mongoose.Schema({
name: String,
third: {type: Schema.Types.ObjectId, ref: 'Third'},
});
var ThirdSchema = new mongoose.Schema({
name: String
});
var First = mongoose.model('First', FirstSchema);
var Second = mongoose.model('Second', SecondSchema);
var Third = mongoose.model('Third', ThirdSchema);
First.remove({}).exec();
Second.remove({}).exec();
Third.remove({}).exec();
var _3 = new Third({name: 'third'});
_3.save(function(err1) {
if (err1) {
throw err1;
}
var _2 = new Second({name: 'second', third: _3.id});
_2.save(function(err2) {
if (err2) {
throw err2;
}
var _1 = new First({name: 'first', second: _2.id});
_1.save(function() {
First.find({}).populate({
path: 'second',
populate: { path: 'third'}
}).exec(function(err, result) {
console.log(result[0]);
});
});
});
});
is there your path
is assigned by a wrong value or something?
it should be a field name, not a object name
Upvotes: 1