這是我在 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)
}
});
}
}
);
uj5u.com熱心網友回復:
你需要在 if 中拋出錯誤
if(error) {
throw error;
} else {
而且我認為錯誤在于您傳遞引數的方式,它應該是一個陣列:
pool.query("SELECT password_hash FROM User WHERE email = ?",
[req.body.EMailLogin],
我希望這可以解決您的問題。
uj5u.com熱心網友回復:
您錯過了檢查天氣查詢回傳資料與否,您也可以使用物件或陣列傳遞值(在您的情況下,應該是陣列)
pool.query("SELECT password_hash FROM User WHERE email = ?", [req.body.EMailLogin],
(error, results, fields) => {
if (error) {
// any error like wrong query, connection closed, etc.
// 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 if (results.length === 0) {
// email not found in database
// My Page for Wrong Email ???
} 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)
}
});
}
}
);
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/340067.html
