我有一個登錄系統,用戶在其中輸入他們的資訊,當他們提交資訊時,我用 express 驗證資訊,如果它無效,我會發送一條錯誤訊息。現在我只是將 res.send 用于錯誤訊息,我將如何重定向回我的表單但有一個錯誤訊息。我寧愿不使用 url 引數,因為那不安全。
uj5u.com熱心網友回復:
所以我的理解是,您希望您的登錄表單顯示錯誤訊息,例如密碼錯誤。
const login = (req, res) => {
const user = new userModel(req.body.email, req.body.password);
const found = db.findUser(user);
if (found) {
if (user.password == found.password) {
res.status(200).send(true);
} else {
res.status(401).json({ msg: 'The password is incorrect'});
}
} else {
res.status(404).send(false);
}
};
然后你可以使用 msg 屬性是密碼在獲取中是錯誤的。
fetch('/api/users/login', {
method: 'POST'
})
.then(res => res.json())
.then(data => {
document.getElementById('...some div').innerHTML = `<div>${data.msg}</div>`
})
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/466382.html
上一篇:帶multer的圖片“未定義”
