0

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.

  • 1
    The error indicates that your results array don't have any record. It's trying to read password from undefined values. – Vikas Keskar Feb 22 '21 at 13:45
  • @Vikas Keskar, But I have records of the password in the database. How do I solve it? If there are no results, the system should tell me that the password is wrong. But I don't get this error message. –  Feb 23 '21 at 03:08
  • Update ```!results``` to ```!results.length ```, because in mysql query with mysql node js connector , you will get empty array if database don't have any records for query. – Vikas Keskar Feb 23 '21 at 03:40
  • let me try that. –  Feb 23 '21 at 03:54
  • I think you are right. I don't get the error message, and the input data is logged to the console are expected. I am going to create the login now. –  Feb 23 '21 at 04:35

0 Answers0