node.js - Object #<Promise> has no method 'catch' -


my user lib have following code register

function register { // validation on lib level  //user instance of user collection user.save()     .then(function(error,records, numberofrecords) {         // got kind of error         if (error) {             next('fail',msg,{error_code : 510, exception : ex} )             return ;                }         next('success','good', records );     })     .catch(function(ex) {         var msg =  ex.message;         error = ex         system.log.error(msg);         next('fail',msg,{error_code : 510, exception : ex} );     }).done(); } 

in mongooes modal, have implemented middle-ware

    user.pre('save', function (next) {            // checking error available in catch schope or not           var err = new error('i doing experiment on it');           next(err);     });  var promise = require('bluebird');  var user = mongoose.model('user'); promise.promisifyall(user); promise.promisifyall(user.prototype); 

getting following issue

typeerror: object #<promise> has no method 'catch'     @ user.register (/server/core/modules/user.js:69:10)     @ router.post.http.request (/server/routes/user.js:55:8)     @ layer.handle [as handle_request] (/node_modules/express/lib/router/layer.js:82:5)     @ next (/node_modules/express/lib/router/route.js:110:13)     @ route.dispatch (/node_modules/express/lib/router/route.js:91:3)     @ layer.handle [as handle_request] (/node_modules/express/lib/router/layer.js:82:5)     @ /node_modules/express/lib/router/index.js:267:22     @ function.proto.process_params (/node_modules/express/lib/router/index.js:321:12)     @ next (/node_modules/express/lib/router/index.js:261:10)     @ function.proto.handle (/node_modules/express/lib/router/index.js:166:3)     @ router (/node_modules/express/lib/router/index.js:35:12)     @ layer.handle [as handle_request] (/node_modules/express/lib/router/layer.js:82:5)     @ trim_prefix (/node_modules/express/lib/router/index.js:302:13)     @ /node_modules/express/lib/router/index.js:270:7     @ function.proto.process_params (/node_modules/express/lib/router/index.js:321:12)     @ next (/node_modules/express/lib/router/index.js:261:10)     @ /server/router.js:45:3 

my question ::
how resolve issue ??
how can catch error mongooes middleware lib function (catch)??

it seems you're not using bluebird, mongoose promises (mpromise), don't have .catch method.

you can fix by

  • using user.saveasync().then(…), you've promisified user model
  • using promise.resolve(user.save()).then(…) cast mongoose promise bluebird one

Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

Kivy: Swiping (Carousel & ScreenManager) -