我正在尝试检查我是否正在获取数据以及密码是否正确。
密码的状况很好,即密码不正确时我会出错。
但是!发现的条件没有运行,浏览器正在连续加载,而不是显示";用户名或密码不正确";。
我两者都要!找到和!passwordOK条件必须有效。但在我的情况下,如果找到的值也为null,浏览器将不停止加载,而不是显示错误消息。
非常感谢提前
**Router.post('/', async(req, res) => {
try{
const loginData = {
email : req.body.email,
password : req.body.password
}
const found = await DB_Collection.findOne({email : loginData.email})
// comparing the password
const passwordOK = await bcrypt.compare(loginData.password, found.password)
if( !found || !passwordOK){
res.send("Inncorrect email or password..!");
}
else{
res.send("Logged in successfully...!");
}
}catch(err){
return err
}
})**
您得到一个TypeError:访问此处的null属性
const passwordOK = await bcrypt.compare(loginData.password, found.password)
毕竟,find为null。它在这里被捕获:
} catch(err) {
return err
}
仅当found
不为空时执行检查。例如,像这样:
const passwordOK = found
? await bcrypt.compare(loginData.password, found.password)
: false;