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)
es2015 (es6):
require('mongoose').Promise = Promise;
bluebird:
require('mongoose').Promise = require('bluebird');
Q:
require('mongoose').Promise = require('q').Promise;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With