mysql - Express JS 查询错误处理不起作用
问题描述
这是我在 Stackoverflow 上的第一篇文章,请善待。
我有一个大问题,我无法通过谷歌搜索来解决(这非常罕见)。我想在我的查询中创建一个简单的错误处理,但它不起作用。
这是代码:
pool.query("SELECT password_hash FROM User WHERE email = ?",
req.body.EMailLogin, (error, results, fields) => {
if(error) {
// It executes the if aswell as the else statement and I dont know why (if(error) seems to be ignored even if its true)
} else {
// And then it crashes here, because password_hash is undefined
let hash = results[0].password_hash;
bcrypt.compare(req.body.PasswordLogin, hash, function(err, result) {
if (result == true) {
ses.user = req.body.EMailLogin;
req.session.cookie.expires = false;
req.session.save(() => {
return res.redirect('/index');
});
} else {
res.render(); // My Page for Wrong Password (ignore this)
}
});
}
}
);
解决方案
Throw
if里面的错误
if(error) {
throw error;
} else {
我认为错误是您传递参数的方式,它应该是一个数组:
pool.query("SELECT password_hash FROM User WHERE email = ?",
[req.body.EMailLogin],
我希望这可以解决您的问题。
推荐阅读
- c# - 将鼠标速度乘数添加到得分
- selenium-webdriver - 由于页面加载时间不同,如何等待找到元素
- azure - 无法使用服务主体创建映射到 Azure AD 标识的 Azure SQL 数据库用户
- javascript - 单击时,Bootstrap 4 单选按钮向左移动
- python - 在 Pandas 中连接时数据未将分隔符应用于 csv 文件
- sql - 如何使用 GO-MSSQLDB 驱动程序获取最后插入的 ID?
- jpanel - 我如何在此代码段中错误地使用 KeyListener?
- c# - 在网格中显示所有类型的上传文件
- angularjs - AngularJS:尝试通过组合变量和“px”来创建样式值
- python - UnsupportedOperation:不能做非零当前相对搜索:Python