Ling
Ling

Reputation: 1093

Bluebird and Mongoose: Warning: a promise was created in a handler but was not returned from it

I use mongoose and Bluebird in my project. This warning is everywhere even I correct all my code. It still happen.

exports.middleware = function (req, res, next, id) {
  Account.findById(id).exec().then(function(account) {
    if (!account) {
      return res.status(404).send({
        message: 'No account with that identifier has been found'
      });
    }
    req.account = account;
    next();
  }).catch(function(err) {
    return next(err);
  });
};

Upvotes: 2

Views: 277

Answers (1)

Ling
Ling

Reputation: 1093

After I check mongoose source code of lib/query.js I noticed there is some issue in the exec() callback function

https://github.com/Automattic/mongoose/blob/master/lib/query.js#L2243

query.prototype.exec = function exec(op, callback) {
  ...
  if (callback) {
    promise.then(
      function() {
        callback.apply(null, _results);
      },
      function(error) {
        callback(error);
      });
  }
  ...
}

There is no return in promise.then(). So before mongoose fix this issue. I just avoid to use exec(callback) in my code. then everyone will be happy.

Upvotes: 2

Related Questions