我正在嘗試查看連接的資料庫是否有一封與用戶發布的電子郵件相匹配的電子郵件:
app.post("/forgot_password", (req, res, next) => {
try {
const { emailId } = req.body;
db.query("SELECT * FROM user WHERE email = ?", async (error, results) => {
if (error) {
console.log(error);
}
if (emailId != results) {
//Change to pop up saying incorrect email
res.render("forgot_password")
return
}
})
} catch (error) {
console.log(error)
}
})
如何檢查“email”列中的任何資料是否與此處發布的“emailId”常量匹配:
<form action="" method="POST">
<label for="email">Email</label>
<input type="email" name="emaild" id="email">
<br>
<input type="submit" value="submit">
</form>
uj5u.com熱心網友回復:
您必須將變數提供給查詢
const { emailId } = req.body;
db.query("SELECT * FROM user WHERE email = ?", [emailId], async (error, results) => {
請注意,我向查詢函式添加了一個引數。它的簽名是:
db.query(sqlString, arrayOfParameters, callbackFuntion)
這是檔案的鏈接: https ://github.com/mysqljs/mysql#performing-queries
該查詢從資料庫中回傳一個行陣列,其中電子郵件列與 emailId 的值匹配。我假設 emailId 是用戶提供的實際電子郵件,例如[email protected]
結果應該只有一行,因為我假設不同的用戶不能擁有相同的電子郵件。
因此,要檢查電子郵件是否匹配,只需驗證行數是否為 1
const { emailId } = req.body;
db.query("SELECT * FROM user WHERE email = ?", [emailId], async (error, results) => {
if (error) {
console.log(error);
return // we want to exit the function
}
// so you can see what the results are
console.log(results)
if (results.length === 1) {
console.log('the user supplied an email that exists in our database')
} else {
console.log('no email found', emailId)
}
})
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/417209.html
標籤:
