Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
293 views
in Technique[技术] by (71.8m points)

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

My user lib have following code for register

function register {
// do some validation on lib level

//user is instance of user collection
user.save()
    .then(function(error,records, numberOfRecords) {
        // got any 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 my mongooes modal, I have implemented middle-ware

    User.pre('save', function (next) {

          // just checking error is available in catch schope or not
          var err = new Error('I am doing some 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'
    at User.register (/server/core/modules/user.js:69:10)
    at router.post.http.request (/server/routes/user.js:55:8)
    at Layer.handle [as handle_request] (/node_modules/express/lib/router/layer.js:82:5)
    at next (/node_modules/express/lib/router/route.js:110:13)
    at Route.dispatch (/node_modules/express/lib/router/route.js:91:3)
    at Layer.handle [as handle_request] (/node_modules/express/lib/router/layer.js:82:5)
    at /node_modules/express/lib/router/index.js:267:22
    at Function.proto.process_params (/node_modules/express/lib/router/index.js:321:12)
    at next (/node_modules/express/lib/router/index.js:261:10)
    at Function.proto.handle (/node_modules/express/lib/router/index.js:166:3)
    at router (/node_modules/express/lib/router/index.js:35:12)
    at Layer.handle [as handle_request] (/node_modules/express/lib/router/layer.js:82:5)
    at trim_prefix (/node_modules/express/lib/router/index.js:302:13)
    at /node_modules/express/lib/router/index.js:270:7
    at Function.proto.process_params (/node_modules/express/lib/router/index.js:321:12)
    at next (/node_modules/express/lib/router/index.js:261:10)
    at /server/router.js:45:3

My Question is ::
How to resolve this issue ??
How can I catch error from mongooes middleware to my lib function (catch)??

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It seems you're not using Bluebird, but mongoose promises (mpromise), which don't have a .catch method.

You can fix that by

  • using user.saveAsync().then(…), as you've promisified the User model
  • using Promise.resolve(user.save()).then(…) to cast the mongoose promise into a Bluebird one

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...