I used Bcryptjs to encrypt a password stored in mysql database. The Problem I have is the login porcess trying to compare the password in the Mysql Database to the login password.The Error message is what I get below.
(node:3616) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'password' of undefined
The enter code is below.
exports.login = async(req,res) => {
try{
//const email = req.body.email;
//const password = req.body.password;
const {email,password} = req.body;
if(!email || !password){
res.status(400).render('login',{message:'Both Email and Password are required!'});
}
db.query('SELECT * FROM users WHERE email=?', [email], async function(error,results){
console.log(results);
//the problem is the compare
if(!results || !(await bcrypt.compare(password,results[0].password))){
res.status(401).render('login',{message:'Email or Password is incorrect'});
}
});
}catch(err){
console.log(err);
}
}
Thanks for the help.