Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript Promises mongoose and bluebird missing catch and fail

I've started using promises, I use Node.js Mango (with mongoose) and bluebird.. The issue I'm having is for some reason when I chain the mongoose call with functions returning promises (I'm assuming this is the correct way to return and chain) then I get:

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

if I change the fail to catch then I get the same issue:

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

what I do is use the function(null, function) pattern which is exactly fail and catch. However the catch / fail is more readable. Any clue why I'm getting this and how I should resolve this issue?

Here is an example of the 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)
like image 963
Dory Zidon Avatar asked May 22 '14 16:05

Dory Zidon


1 Answers

mongoose 4.1+ maintainer suggestion:

es2015 (es6):

require('mongoose').Promise = Promise;

bluebird:

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

Q:

require('mongoose').Promise = require('q').Promise;
like image 57
King Friday Avatar answered Oct 13 '22 22:10

King Friday