mongoose: document.execPopulate() not returning promise

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

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Comments: 27

Most upvoted comments

@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.

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.