我正在学习如何在非常基础的水平上使用Express,并且在尝试检查空表单字段时遇到了问题。
目前,如果我不使用bcrypt,在检查空密码和电子邮件字段时,我能够成功重定向到我想要的位置。但是,当合并bcrypt时,它适用于检查空电子邮件和密码,用于检查填写了密码的空电子邮件,但不能用填写的电子邮件检查空密码。
如果您需要任何其他代码来检查问题,请告诉我,我会更新。
//Post register
app.post("/register", (req, res) => {
var email = req.body.email;
//This doesn't work when checking for empty password with filled email
// var password = bcrypt.hashSync(req.body.password, 10);
//This works without bcrypt
var password = req.body.password;
//Validation
for (key in users) {
if (users[key].email === email) {
res.status(400).send('This email is already in use');
return;
}
}
if (email === '' || password === '') {
res.status(400).send('Please enter a valid email and password');
return;
}
var randomID = generateRandomString();
users[randomID] = {id: randomID, email: email, password: password};
req.session.user_id = randomID;
res.redirect('/urls');
});