【发布时间】:2016-10-07 07:11:04
【问题描述】:
我正在为我的 Web 应用程序使用sails js。我必须更改 beforeCreate 的默认行为。先看代码:
beforeCreate: function(values, next) {
//ParamsCheck is my service and 'check' is method which will validate the
//parameters and if any invalid parameter then error will be thrown, otherwise
//no error will be thrown
ParamsCheck.check(values)
.then(() => {
// All Params are valid and no error
next();
})
.catch((err) => {
//Some errors in params, and error is thrown
next(err);
});
}
所以,问题是如果有任何错误,那么下一个方法会自动重定向到 serverError,错误代码为 500,而我想用我的自定义响应重定向它(例如:badRequest,错误代码 400)。如何做到这一点?
【问题讨论】:
-
收到此错误后如何发送响应,能否显示调用最终回调的代码?
-
其实在sails中,如果给beforeCreate方法的next()回调传入任何参数,就会报错,报500错误。 'next()' 是最后的回调。我想更改默认响应。
-
当你传递错误时会回调,它会给你500 statusCode。对于错误的请求意味着statusCode:400你需要在响应客户端时传递