JavaScript Promises mongoose and bluebird lack catch and crash

I started using promises, I use Node.js Mango (from mongoose) and bluebird .. The problem I am facing is for some reason when I associate a mongoose call with functions that return promises (I assume this is the right way return and chain), I get:

typeError: Object #<Promise> has no method 'fail' 

If I change the error to catch, I get the same problem:

 typeError: Object #<Promise> has no method 'catch' 

I am using a function template (null, function) that definitely fails and gets caught. However, catch / fail is more readable. Any clue why I am getting this and how should I solve this problem?

Here is an example code block.

 User.findOne({ 'email' : user_email }).exec() }).then (promisedTransformUserSchemaToFrontendObjectWithProjectMapping) .then (function (feUser) { return new Promise(function (resolve, reject) { res.json(feUser); return resolve(feUser); }); }).fail/catch (function (err) { console.log(err); sendError(res,"failed to get user",err); }); 

And here is the stacktrace:

 TypeError: Object #<Promise> has no method 'catch' at module.exports.app.put.User.update.email (app\controllers\router.js:165:16) at callbacks (node_modules\express\lib\router\index.js:164:37) at isLoggedIn (app\controllers\router.js:741:10) at callbacks (node_modules\express\lib\router\index.js:164:37) at param (node_modules\express\lib\router\index.js:138:11) at param (node_modules\express\lib\router\index.js:135:11) at pass (node_modules\express\lib\router\index.js:145:5) at Router._dispatch (node_modules\express\lib\router\index.js:173:5) at Object.router (node_modules\express\lib\router\index.js:33:10) at next (node_modules\express\node_modules\connect\lib\proto.js:193:15) 
+8
javascript promise mongodb mongoose
source share
4 answers

mongoose 4.1+ accompanying offer:

es2015 (es6):

 require('mongoose').Promise = Promise; 

Bluebird:

 require('mongoose').Promise = require('bluebird'); 

IN:

 require('mongoose').Promise = require('q').Promise; 
+15
source share

I don’t know what moongose ​​is, but in general, functions like fail or catch are convenient shortcuts and are not part of the promises specification. Therefore, the library does not need to be promises -compliant. Apparently, in your case they are not there.

You can achieve the same functionality with then(successHandler, rejectionHandler) .

To handle the rejection of promises, you can rewrite your code as follows:

 User.findOne({ 'email' : user_email }).exec() }).then (promisedTransformUserSchemaToFrontendObjectWithProjectMapping) .then (function (feUser) { return new Promise(function (resolve, reject) { res.json(feUser); return resolve(feUser); }); }).then (undefined, function (err) { console.log(err); sendError(res,"failed to get user",err); }); 
+5
source share

Another way to do this is shown in bluebird docs:

https://github.com/petkaantonov/bluebird/blob/master/API.md#promiseresolvedynamic-value---promise

You can wrap the mongoose promise in bluebird Promise.resolve () and you will return the blue bird promise.

  Promise.resolve(User.findOne({ 'email' : user_email }).exec()) .then (promisedTransformUserSchemaToFrontendObjectWithProjectMapping) .then (function (feUser) { res.json(feUser); return feUser; }).fail/catch (function (err) { console.log(err); sendError(res,"failed to get user",err); }); 
+3
source share

It seems that the problem was mixing the two types of promises (bluebird and mongoose).

As soon as I used promsifyAll on the db object, the catch started working.

 // promisify all model using mongoomise.. require('../../mongoomise').promisifyAll(mongoose, require('bluebird')) 
0
source share

All Articles