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.

Can't make nested populate in new mongoose 3.6rc0

See original GitHub issue

I have the following schemas:

var DocumentSchema = new Schema({
  title    : {type: String},
  emails   : [{type: ObjectId, ref: 'Emails'}]
});

var EmailSchema = new Schema({
  title    : {type: String},
  tags     : [{type: ObjectId, ref: 'Tags'}]
});

var TagSchema = new Schema({
  title    : {type: String}    
});

I want to find a document with populated ‘emails’ and tags. When I try this:

Document.findById(ObjectId('...'))
  populate('emails.tags')
  .exec(function (err, document) {

  });

I get the error:

Caught exception: TypeError: Cannot call method 'path' of undefined

Issue Analytics

  • State:closed
  • Created 11 years ago
  • Comments:19

github_iconTop GitHub Comments

12reactions
aheckmanncommented, Apr 4, 2013

@yangsu you are correct. Though there is another way. Either specify the model that should be used for population in the options to User.populate or use the Phone model directly:


BlogPost.find({ tags: 'fun' }).lean().populate('author').exec(function (err, docs) {
  assert.ifError(err);

  User.populate(docs, {
    path: 'author.phone',
    select: 'name',
    model: Phone // <== We are populating phones so we need to use the correct model, not User
  }, callback);

  // or use the model directly

  Phone.populate(docs, {
    path: 'author.phone',
    select: 'name',
  }, callback);

This seems clunky. We should be able to do better with this in a future release. If the documents being populated are mongoose documents (opposed to plain objects) we could possibly use it’s related schema to determine which Model to use. Won’t work for plain objects of course.

4reactions
ezequielzaccacommented, Apr 21, 2017

As the metadata for population is present in the ref property, shouldnt the libary be able to automatically guess the correct model for population?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Populate Nested Array In Mongoose - Node.Js - ADocLib
This tutorial gives you three ways to create and navigate to different screens using React cannot be used in embedded scripts unless such...
Read more >
Mongoose でネストした populate の書き方 - Qiita
Mongoose の 3.6 から ネストした populate に対応したそうなので動作確認を試みたのです ... Can't make nested populate in new mongoose 3.6rc0.
Read more >
mongooseを使ってmongodbでリレーションとjoinっぽいこと
mongoose の場合、Schema 定義の部分で ref を使うことでリレーションを表現 ... Can't make nested populate in new mongoose 3.6rc0 · Issue #1377 ...
Read more >
Shared tabs - OneTab
javascript - Node.js - Creating Relationships with Mongoose - Stack Overflow ... Can't make nested populate in new mongoose 3.6rc0 · Issue #1377 ......
Read more >
Mongoose find nested object in array
Mongoose helps you create nested schemas when you nest an object in another object . // This code is the same as above...
Read more >

github_iconTop Related Medium Post

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