Tal Gvili
Tal Gvili

Reputation: 319

Mongoose and BlueBird return from promise

I use mongoose and bluebird as a promise framework. Everytime I use "save" or "remove" I get this error :

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

I really tried spending on it a few days, while googling, I tried so much ways, to mention some:

  1. Creating a promise and resolve it in save/remove CB;
  2. putting 'return' in so many logical combinations to make sure it always 'return' from a promise.
  3. Creating functions in the model, and naming it "saveAsync" (I saw it in one example) and there doing all the promise handling.
  4. Kinda funny but I tried updating all my project npm packages, because I saw talks about it in github and someone mentioned they solved it already. but it didn't work.

    And much more.. I'm really desperate.
    Don't get me wrong, the code works great, but seeing this HUGE warnings in my console each time makes me feel really guilty.

Any suggestions?

Upvotes: 0

Views: 175

Answers (1)

Benjamin Gruenbaum
Benjamin Gruenbaum

Reputation: 276596

This error means some code did something like:

somePromise.then(x => {
    someOtherPromiseReturningFunction();
}).then(value => {
   // forgot a return, oh dear
});

Which is a very common mistake of forgetting a return, it messes with error handling and causes issues.

Sometimes the problem is not with your code but with the code of a library you're using - in that case you should disable warnings for that code:

  • Require bluebird separately for your own code and for mongoose via require("bluebird") and use it with warnings.
  • Disable warnings for the copy mongoose uses.

You can get two copies of bluebird by using require("bluebird") in your code and overriding mongoose's Promise with require("bluebird/js/release/promise")(); which creates a standalone copy.

Upvotes: 1

Related Questions