mongoose: 4.6.6
mongodb: 2.2.11
node: 4.5
Just enabling/disabling the virtual populate field results in array subdocuments showing as null
in query results
var someModelSchema = new mongoose.Schema({
name: String
});
mongoose.model('someModel', someModelSchema)
var schema0 = new mongoose.Schema({
name0: String
});
schema0.virtual('detail', {
ref: 'SomeModel',
localField: '_id',
foreignField: '_id',
justOne: true
});
var schema1 = new mongoose.Schema({
name1: String,
list: [schema0],
obj: schema0
});
var schemaMain = new mongoose.Schema({
nestedObj: schema1,
nameMain: String
});
var ModelMain = mongoose.model('ModelMain', schemaMain);
var x = new ModelMain({
nameMain: 'sss',
nestedObj: {
name1: 'name1',
list: [{
name0: 'name0.1'
}],
obj: {
name0: 'name0.1'
}
}
})
x.save().then(function (err, docs) {
ModelMain.find({}, function (err, docs) {
console.log(JSON.stringify(docs, null, 4));
})
})
results in
{
"_id": "581cbf70d7c120443684a490",
"nameMain": "sss",
"nestedObj": {
"name1": "name1",
"obj": {
"name0": "name0.1",
"_id": "581cbf70d7c120443684a492"
},
"_id": "581cbf70d7c120443684a491",
"list": [
null
]
},
"__v": 0
}
the list
property is null. If you comment out the virtual property declaration then list contains expected data, which is:
{
"_id": "581cbfdf0700ba445223c848",
"nameMain": "sss",
"nestedObj": {
"name1": "name1",
"obj": {
"name0": "name0.1",
"_id": "581cbfdf0700ba445223c84a"
},
"_id": "581cbfdf0700ba445223c849",
"list": [
{
"name0": "name0.1",
"_id": "581cbfdf0700ba445223c84b"
}
]
},
"__v": 0
}
This problem is no longer there after commenting out the pre-hook in Schema.prototype.virtual
declaration in schemas.js
Even after replacing the hook with an empty one:
this.pre('init', function(next, obj) {
next();
});
the issue is still there.
All is fine if this is commented out.
Fixed in d09c3d677ca4daf087be343af177f05ae2043502
Most helpful comment
Fixed in d09c3d677ca4daf087be343af177f05ae2043502