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.

document.execPopulate() not returning promise

See original GitHub issue

My promise chain looks like this:

myDocument.save()
  .then(function (savedDocument) {
    return MyModel.update(/*query, options*/).exec();
  })
  .then(function () { 
    return myDocument
      .populate(/*path, select, model*/)
      .populate(/*path, select, model*/)
      .execPopulate(); // server/controllers/my-document.server.controller:236:10
  })
  .then(function (populatedDoc) {
    //do stuff with populatedDoc
  });

However, I’m getting the following stack trace:

TypeError: undefined is not a function
    at Object.populate (node_modules/mongoose/lib/document.js:2028:22)
    at Object.Document.execPopulate (node_modules/mongoose/lib/document.js:2070:8)
    at server/controllers/my-document.server.controller.js:236:10
    at newTickHandler (node_modules/mongoose/node_modules/mpromise/lib/promise.js:229:18)
    at wrapped (node_modules/newrelic/lib/transaction/tracer/index.js:157:28)
    at process._tickDomainCallback (node.js:381:11)
    at process.wrappedFunction (node_modules/newrelic/lib/transaction/tracer/index.js:262:51)

which, looking at the source code, means that the Document object’s constructor does not have a populate method? scratch that, I’m quite confused, and didn’t find anything on S/O about this… I’ll happily move this question there, but this looks to me like a bug since the documentation says I should be able to do this.

For reference I’m using Mongoose v4.0.x

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:27

github_iconTop GitHub Comments

3reactions
vkarpov15commented, Mar 31, 2020

@swordsreversed execPopulate() is a function on a document, not a query. So the below requires execPopulate():

Podcast.findOne({}, function(error, doc) {
  // Query calls back with a document which has a [populate](https://masteringjs.io/tutorials/mongoose/populate) method.
  var promise = doc.populate('categories').execPopulate();
});

Your approach attaches populate to the query, so what you need is:

var promise = Podcast.findOne({}).populate('categories').exec();

Please review the promises docs for more info.

2reactions
swordsreversedcommented, Feb 12, 2016

Hi I’m getting a similar problem with

return Podcast.findOne({ slug: params[0]})
      .populate('categories')
      .execPopulate();

returning a stacktrace of

TypeError: _modelsPodcast2.default.findOne(...).populate(...).execPopulate is not a function

Also i’m putting in

mongoose.Promise = global.Promise;

to use ES6 through Babel. Mongoose is 4.4.3. Not really sure how to proceed after reading this thread. Any help would be appreciated.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Does Model.populate() return a promise? - node.js
I have two code snippets, both use populate() on a Model however one code snippet requires execPopulate() to return a promise but the...
Read more >
Document.prototype.unmarkModified() - Mongoose
If the path was not provided, then all populated fields are returned to their unpopulated state. Document.prototype.directModifiedPaths(). Returns: «Array».
Read more >
Mongoose API v4.4.14
Mongoose#Document() ... When no collection argument is passed, Mongoose produces a collection name by ... execPopulate() // executed, returns promise ...
Read more >
Creating a Promise Helper Function for your Mongoose App
execPopulate() .then(user => { res.render('newDesign/blog', { cart_items: user.cart.items, ... getShoppingCartData = req => { return new Promise((resolve, ...
Read more >
文档API - Mongoose文档
Called internally after a document is returned from mongodb. ... If you want to use promises instead, use this function with execPopulate(). 示例....
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