nested populate virtuals returning null
See original GitHub issueHi Folks,
Continue to struggle with populating virtual fields nested in an array
'use strict';
//var mongoose = require('mongoose');
var Schema = mongoose.Schema;
//mongoose.connect('mongodb://localhost/test');
var options = {
toObject: {getters: true},
toJSON: {getters: true}
};
var BSchema = new Schema({
name: String,
c_id: String
}, options);
BSchema.virtual('cs', { // <-- comment this block in then the b_list array is null
ref: 'C',
localField: 'c_id',
foreignField: 'fk_c_id'
});
var ASchema = new Schema({
name: String,
b_list: {
type: [BSchema]
}
}, options);
var CSchema = new Schema({
name: String,
fk_c_id: {type: String, ref: 'B'}
}, options);
var A = mongoose.model('A', ASchema);
var B = mongoose.model('B', BSchema);
var C = mongoose.model('C', CSchema);
A.find().populate({
path: 'b_list',
model: 'B',
populate: {
path: 'cs',
model: 'C'
}
}).exec().then(function (result) {
console.log(JSON.stringify(result, null, 4));
});
return;
// to populate
var c = new C({name: 'C1', fk_c_id: 'c_id_1'});
c.save().then(function (c) {
var b = new B({name: 'B1', c_id: 'c_id_1'});
return b.save();
}).then(function (b) {
var a = new A({name: 'A1', b_list: [ b ]});
return a.save();
}).then(function () {
A.find().populate({
path: 'b_list',
populate: {
path: 'cs'
}
}).exec().then(function (result) {
console.log(JSON.stringify(result, null, 4));
});
}).catch(function (err) {
return console.log(err.message);
});
Without BSchema.virtual(‘cs’, { …
> [
{
"_id": "57aa4870415890f84857e260",
"name": "A1",
"__v": 0,
"b_list": [
{
"_id": "57aa4870415890f84857e25f",
"c_id": "c_id_1",
"name": "B1",
"__v": 0,
"id": "57aa4870415890f84857e25f"
}
],
"id": "57aa4870415890f84857e260"
}
]
but when I attempt to populate b_list (array of B) and access the child cs I am getting nulls
BSchema.virtual('cs', { ...
> [
{
"_id": "57aa4870415890f84857e260",
"name": "A1",
"__v": 0,
"b_list": [
null
],
"id": "57aa4870415890f84857e260"
}
]
I’m hoping this is something to do with my populate. Can someone suggest a fix or verify an issue?
To repro, comment in and out the BSchema.virtual(‘cs’, { …
Populate the first time by commenting in the the return; short circuit.
Thanks to you all!
Issue Analytics
- State:
- Created 7 years ago
- Comments:10
Top Results From Across the Web
nested populate virtuals returning null · Issue #4278 - GitHub
I'm not sure if this is related to another issue, but I am struggling with populating virtuals in a nested array.
Read more >Mongoose nested populate virtual return null - Stack Overflow
Why does virtual in a nested populate return null? I have a Post schema and a User schema like this: Post Schema:
Read more >Mongoose virtual field for nested field returning null-mongodb
Coding example for the question Mongoose virtual field for nested field returning null-mongodb.
Read more >The deep virtual population in mongoose is actually very simple!
mongoose's virtual population is very useful tool. ... is not populated (or asked to populate photos path), it will have photo field with...
Read more >Mongoose v6.8.1: SubDocuments
What is a Subdocument? Subdocuments are similar to normal documents. Nested schemas can have middleware, custom validation logic, virtuals, and any other ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Perhaps because your user has an
_fid
field?The issue addressed with #4683