未定义的Restify返回值不是错误对象中的函数

我有一个下面的Restify自定义错误,正在抛给我的BlueBird Promise catch块。

var test = function() { respObject = { hello: { world: 'aasas' } }; throw new restify.errors.ServiceError(respObject, 422); } 

然后在ServiceError中:

 function ServiceError(respObject, statusCode) { restify.RestError.call(this, { restCode: 'ApiError', statusCode, statusCode, message: 'Api Error Occurred', constructorOpt: ServiceError, body: { message: 'Api Error Occurrede', errors: respObject.toJSON() } }); this.name = 'CustomApiError'; } util.inherits(ServiceError, restify.RestError); restify.errors.ServiceError = ServiceError; 

但是在test()的调用函数中:

 test().catch(function(err) { console.log(err); }); 

它返回undefined is not a function 。 catch块中的上述调用函数没有返回err对象吗?

问题不在于Restify,而在于testfunction。 你正在调用test().catch ,但是test()不会返回任何东西,也就是说它返回undefined 。 你基本上调用undefined.catch ,它不存在。

如果你想调用catchtest结果,它需要返回一个promise。

我find了答案。 这是respObject.toJSON() ,导致undefined is not a function因为respObject不具有toJSON()函数:)