对象#<承诺>没有方法'catch' [英] Object #<Promise> has no method 'catch'
本文介绍了对象#<承诺>没有方法'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();
}
在mongooes模式中,我已经实现了中间件
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);
获取以下问题
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
我的问题是::
如何解决此问题?
如何从mongooes中间件捕获到我的lib函数的错误(捕获)?
My Question is ::
How to resolve this issue ??
How can I catch error from mongooes middleware to my lib function (catch)??
推荐答案
似乎您没有使用Bluebird,但是猫鼬承诺( mpromise ),其中没有.catch
方法.
It seems you're not using Bluebird, but mongoose promises (mpromise), which don't have a .catch
method.
您可以通过以下方式解决此问题
You can fix that by
- 使用
user.saveAsync().then(…)
,因为您已经证明了User
模型 - 使用
Promise.resolve(user.save()).then(…)
将猫鼬的诺言变成蓝鸟的诺言
- using
user.saveAsync().then(…)
, as you've promisified theUser
model - using
Promise.resolve(user.save()).then(…)
to cast the mongoose promise into a Bluebird one
这篇关于对象#<承诺>没有方法'catch'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文