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.

Populate Virtuals : not working 😞

See original GitHub issue

Hi, I am using mongoose 4.5.3. As mentioned in the docs, http://mongoosejs.com/docs/populate.html in Populate Virtuals, I can’t produce desired results as mentioned.

There isn’t any members field present inside return bands doc. Are you sure this worked with you? Can you please add a travis-ci tests for this?

My code:

var mongoose = require('mongoose');
mongoose.connect('mongodb://127.0.0.1/test');
var db = mongoose.connection;

/**************************************/

var personSchema = new mongoose.Schema({
  name: String,
  band: String
});

var bandSchema = new mongoose.Schema({
  name: String
});


bandSchema.virtual('members', {
  ref: 'Person', // The model to use
  localField: 'name', // Find people where `localField`
  foreignField: 'band' // is equal to `foreignField`
});

var Person = mongoose.model('Person', personSchema);
var Band = mongoose.model('Band', bandSchema);

var person = new Person({
    name : 'Uday',
    band : 'gnr'
});
person.save(function(){
  var band = new Band({
      name : 'gnr'
  });

  band.save(function(){
    Band.find({}).populate('members').exec(function(error, bands) {
      console.log(bands);
    });
  });
});

Runnable

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:4
  • Comments:7

github_iconTop GitHub Comments

11reactions
thatisudaycommented, Jul 21, 2016

Ok. Got it. You must need to set toObject : {virtuals:true}, option in your schema. I guess, you should mention that in the doc. That would be helpful.

5reactions
thatisudaycommented, Jul 21, 2016

Got it, you also need to add option toJSON : {virtuals : true}.

Read more comments on GitHub >

github_iconTop Results From Across the Web

why Virtual Populate not working on Node js and ...
But the problem is my virtual properties (In Product Model) not working as it doesnt show review of user in json format when...
Read more >
Solved: Referencing a variable that hasn't been populated
The main problem arises when I try to call my Power Automate flow, which is configured to take both of the variables.
Read more >
Solved: How can I auto populate email field, when user fie...
Solved: Dear Expert, How can I proceed with this requirement? Once the user field name is entered, the email field should automatically populate....
Read more >
Solved: Merge Queries is not populating the information fo...
Data types are critical and can cause all sorts of issues if not set up properly, especially for merges and relationships in the...
Read more >
How to populate a Value List with very variable data?
The Virtual List technique would work best for the the second method. ... and not have to write a pile of conditions around...
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