question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

nested populate virtuals returning null

See original GitHub issue

Hi Folks,

Continue to struggle with populating virtual fields nested in an array

#4278 #4262

'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:closed
  • Created 7 years ago
  • Comments:10

github_iconTop GitHub Comments

1reaction
vkarpov15commented, Oct 26, 2016

Perhaps because your user has an _fid field?

0reactions
svnindiacommented, Nov 14, 2016

The issue addressed with #4683

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found