Sails JS 助手错误解决/拒绝
Sails JS helpers error resolving/ rejecting
我正在使用 Sails 中的帮助程序进行登录。
这是我的 helpers/login.js:
fn: async function (inputs, exits) {
const password = inputs.password;
const email = inputs.email;
// find user with email
const user = await User.findOne({email});
if (user){
const salt = user.salt;
const hashedPass = user.password;
const iterations = user.iterations;
// check if input password matches with password in DB
crypto.pbkdf2(password, salt, iterations, 64, 'sha512',
(err, key) => {
if (!err) {
if (hashedPass === key.toString('hex')) {
// password matched
return exits.success({code: 200});
}
}
});
}
// account not found or password doesnt match
return exits.success({code: 404});
}
UserController.js:
login: async function(req, res){
let loginUser;
try {
loginUser = await sails.helpers.login.with({
email: req.body.email, password: req.body.password
});
} catch (e) {
console.log("Error login in usercontroller ", e.message);
return res.serverError();
}
if (loginUser.code == 200) {
return res.ok();
}else {
return res.serverError();
}
}
当我有 right email 和 password 时,问题出在 Helper 上意味着 return 一个 code: 200
虽然它 return s code: 404
。来自节点的错误消息:
WARNING: Something seems to be wrong with this function.
It is trying to signal that it has finished AGAIN, after
already resolving/rejecting once.
(silently ignoring this...)
,当我输入错误的用户名/通过电子邮件将其发送给 return 时也是如此。但是当我删除 return exits.success({code: 404})
并输入正确的电子邮件和密码时,它 return 是正确的代码 (200)。我需要帮助修复它。
问题可能是您试图 return 错误代码作为 'success' 退出。在你的 exits
对象中,你应该创建一个像 notFound: { responseType: 'notFound' }
这样的对象,然后当你想在你的代码中使用它时,你可以做 return exits.notFound()
。这是一个例子:https://sailsjs.com/documentation/concepts/actions-and-controllers#?actions-2
我正在使用 Sails 中的帮助程序进行登录。 这是我的 helpers/login.js:
fn: async function (inputs, exits) {
const password = inputs.password;
const email = inputs.email;
// find user with email
const user = await User.findOne({email});
if (user){
const salt = user.salt;
const hashedPass = user.password;
const iterations = user.iterations;
// check if input password matches with password in DB
crypto.pbkdf2(password, salt, iterations, 64, 'sha512',
(err, key) => {
if (!err) {
if (hashedPass === key.toString('hex')) {
// password matched
return exits.success({code: 200});
}
}
});
}
// account not found or password doesnt match
return exits.success({code: 404});
}
UserController.js:
login: async function(req, res){
let loginUser;
try {
loginUser = await sails.helpers.login.with({
email: req.body.email, password: req.body.password
});
} catch (e) {
console.log("Error login in usercontroller ", e.message);
return res.serverError();
}
if (loginUser.code == 200) {
return res.ok();
}else {
return res.serverError();
}
}
当我有 right email 和 password 时,问题出在 Helper 上意味着 return 一个 code: 200
虽然它 return s code: 404
。来自节点的错误消息:
WARNING: Something seems to be wrong with this function.
It is trying to signal that it has finished AGAIN, after
already resolving/rejecting once.
(silently ignoring this...)
,当我输入错误的用户名/通过电子邮件将其发送给 return 时也是如此。但是当我删除 return exits.success({code: 404})
并输入正确的电子邮件和密码时,它 return 是正确的代码 (200)。我需要帮助修复它。
问题可能是您试图 return 错误代码作为 'success' 退出。在你的 exits
对象中,你应该创建一个像 notFound: { responseType: 'notFound' }
这样的对象,然后当你想在你的代码中使用它时,你可以做 return exits.notFound()
。这是一个例子:https://sailsjs.com/documentation/concepts/actions-and-controllers#?actions-2