我正在使用Sails中的Helpers进行登录。这是我的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();
}
}
问题在于Helper,当我拥有权限电子邮件和密码时,它意味着返回code: 200
,尽管它返回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 exits.success({code: 404})
并输入正确的电子邮件和密码时,它会返回正确的代码(200(。我需要帮忙修理。
问题可能是您试图将错误代码作为"成功"退出返回。在exits
对象中,您应该创建一个类似notFound: { responseType: 'notFound' }
的对象,然后当您想在代码中使用它时,您可以执行return exits.notFound()
。以下是一个示例:https://sailsjs.com/documentation/concepts/actions-and-controllers#?actions-2