对象 #<Promise> 没有方法 'catch'
Object #<Promise> has no method 'catch'
我的用户库有以下注册代码
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 模态中,我实现了中间件
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 函数(catch)?
您似乎没有使用 Bluebird,但 mongoose promises (mpromise), which don't have a .catch
method。
您可以通过
解决这个问题
- 使用
user.saveAsync().then(…)
,因为您已经承诺 User
模型
- 使用
Promise.resolve(user.save()).then(…)
将 mongoose promise 转换为 Bluebird one
我的用户库有以下注册代码
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 模态中,我实现了中间件
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 函数(catch)?
您似乎没有使用 Bluebird,但 mongoose promises (mpromise), which don't have a .catch
method。
您可以通过
解决这个问题- 使用
user.saveAsync().then(…)
,因为您已经承诺User
模型 - 使用
Promise.resolve(user.save()).then(…)
将 mongoose promise 转换为 Bluebird one