gpt4 book ai didi

javascript - beforeCreate 和其他生命周期回调响应中出现 E_UNKNOWN 错误

转载 作者:行者123 更新时间:2023-12-03 07:37:45 26 4
gpt4 key购买 nike

在我的 sails.js 应用程序模型中,我想从生命周期回调发送“错误请求”错误代码 400。这是我的 beforeCreate 函数:

beforeCreate: function (values, next) {
if (!values.email) {
next();
} else{
var err = {
httpCode: 400,
message: 'email not defined.'
};
return next(err);
}
}

不是发送 400 错误,而是发送 500 错误。这是输出:

error: Sending 500 ("Server Error") response: 
Error (E_UNKNOWN) :: Encountered an unexpected error
at new WLError (/app/node_modules/sails/node_modules/waterline/lib/waterline/error/WLError.js:25:15)
at duckType (/app/node_modules/sails/node_modules/waterline/lib/waterline/error/index.js:66:10)
at errorify (/app/node_modules/sails/node_modules/waterline/lib/waterline/error/index.js:39:10)
at wrappedCallback (/app/node_modules/sails/node_modules/waterline/lib/waterline/utils/normalize.js:325:15)
at callback.error (/app/node_modules/sails/node_modules/waterline/node_modules/switchback/lib/normalize.js:42:31)
at _switch (/app/node_modules/sails/node_modules/waterline/node_modules/switchback/lib/factory.js:56:28)
at /app/node_modules/sails/node_modules/waterline/lib/waterline/query/dql/create.js:73:23
at /app/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:726:13
at /app/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:52:16
at /app/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:264:21
at /app/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:44:16
at /app/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:723:17
at /app/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:167:37
at /app/node_modules/sails/node_modules/waterline/lib/waterline/query/validate.js:73:23
at /app/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:726:13
at /app/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:52:16
at /app/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:264:21
at /app/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:44:16
at /app/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:723:17
at /app/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:167:37
at /app/node_modules/sails/node_modules/waterline/lib/waterline/query/validate.js:35:27
at /app/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:52:16

Details: { httpCode: 400, message: 'email not defined.' }

我怎样才能实现我的目标?

最佳答案

因此您可以看到收到的错误的最后一行是

详细信息:{ httpCode:400,消息:“电子邮件未定义。” }

因此,当我们在 sails 的生命周期回调中发送错误对象时,waterline 会将其自己的实现包装在我们发送的错误上

在响应中我们得到一个给定格式的字符串

详细信息:{ httpCode:400,消息:“电子邮件未定义。” }//这是字符串

该字符串存在于错误对象的详细信息属性中

所以

假设您的生命周期回调是

beforeValidate: function(values,cb){
cb(JSON.stringify({ httpCode: 400,
code: 997,
message: 'username already exists',
context: 'user create',
custom: true }));
}

尝试对您发送的错误对象进行字符串化

在 Controller 中,您可以以以下格式的字符串形式收到此错误

详细信息:{您定义的错误对象}

现在在 Controller 中

//这个错误对象是在 Promise 的回调或 catch 中接收的

if(error.hasOwnProperty('details')){
var indexStart=error.details.indexOf('{');
//this is where we are extracting the error code that we set inside lifecycle callback
var data =error.details.substring(indexStart);
res.json(500,JSON.parse(data));
}else{
res.json(500,{message : "your message",status : 'your status',data : error});
}

关于javascript - beforeCreate 和其他生命周期回调响应中出现 E_UNKNOWN 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35542697/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com