在环回返回之前设置状态代码
set status code before returning in loopback
我继承了一些非常混乱的环回代码,我想知道是否可以 return 一个不同于 200 的状态码。
例如我有以下代码:
PensionsUser.loginView = function (ctx, credentials, cb) {
var respJSON = util.getresponseTemplate();
credentials.email = credentials.email.toLowerCase().trim();
PensionsUser.login(credentials, 'user', function (err, loginResp) {
if (err) {
util.addErrorToResponse(err, respJSON, 'NO-PUS-LV-001');
app.log.error(util.loggingTemplate('PensionsUser', 'loginView', 'NO-PUS-LV-001', credentials.email));
ctx.res.status = 401; //does not work
cb(null, respJSON);
//etc.
我知道 cb(null, respJSON)
应该像这样 return 编辑错误 cb(respJSON)
但遗憾的是前端代码依赖于此 JSON 被 return 编辑为目前是这样,我的第一步就是更改状态代码。
这可能吗?
对于错误,您可以这样设置状态:
callback({status: 401, message: 'Your error message'});
对于成功状态代码,您已经有人在此处回答:
您可以设置自定义错误,例如 {error:500, message: "Custom Error"} 或者您可以在中间件配置文件中配置 "strong-error-handler"。
要根据自己的要求发送响应代码,可以使用此代码:
"YourModelName"."YourRemoteMethod" = function("YourAcceptArguments", callback) {
var error = new Error("New password and confirmation do not match");
error.status = "Enter the resposne code that you want to send";
// Example => error.status = 400
return callback(error);
};
我继承了一些非常混乱的环回代码,我想知道是否可以 return 一个不同于 200 的状态码。
例如我有以下代码:
PensionsUser.loginView = function (ctx, credentials, cb) {
var respJSON = util.getresponseTemplate();
credentials.email = credentials.email.toLowerCase().trim();
PensionsUser.login(credentials, 'user', function (err, loginResp) {
if (err) {
util.addErrorToResponse(err, respJSON, 'NO-PUS-LV-001');
app.log.error(util.loggingTemplate('PensionsUser', 'loginView', 'NO-PUS-LV-001', credentials.email));
ctx.res.status = 401; //does not work
cb(null, respJSON);
//etc.
我知道 cb(null, respJSON)
应该像这样 return 编辑错误 cb(respJSON)
但遗憾的是前端代码依赖于此 JSON 被 return 编辑为目前是这样,我的第一步就是更改状态代码。
这可能吗?
对于错误,您可以这样设置状态:
callback({status: 401, message: 'Your error message'});
对于成功状态代码,您已经有人在此处回答:
您可以设置自定义错误,例如 {error:500, message: "Custom Error"} 或者您可以在中间件配置文件中配置 "strong-error-handler"。
要根据自己的要求发送响应代码,可以使用此代码:
"YourModelName"."YourRemoteMethod" = function("YourAcceptArguments", callback) {
var error = new Error("New password and confirmation do not match");
error.status = "Enter the resposne code that you want to send";
// Example => error.status = 400
return callback(error);
};