编辑:我尝试了天真的方法并且成功了!您可以将不成功退出作为函数返回,并传递 json 作为参数。示例代码:
return exits.notfound({
error: true,
message: 'The *thing* could not be found in the database.'
});
原答案:
您可以从操作 2 访问响应对象,并将错误代码和消息放在那里。
在您的退出中,只需设置您想要的 statusCode ,并在操作本身中根据特定的退出修改您的 res ,然后再抛出它。
...
exits: {
notFound: {
statusCode: 403,
description: 'not found'
}
...
并在你的行动中:
...
if(!userRecord) {
this.res.message =
{
exit: 'notFound',
message: 'The *thing* could not be found in the database.'
};
throw 'notFound';
}
...
您可以设置自定义响应来执行相同的操作。将 responseType 放入操作 2 出口中,如下所示:
...
exits: {
notFound: {
responseType: 'notfound',
description: 'not found'
}
...
然后在 api/responses 中创建自定义响应并在那里设置状态代码和消息。
...
module.exports = function notfound() {
let req = this.req;
let res = this.res;
sails.log.verbose('Ran custom response: res.notfound()');
res.message =
{
exit: 'notFound',
message: 'The *thing* could not be found in the database.'
};
return res.status(403);
}
...