'What is wrong with my code? I am trying to authenticate a user login with expressjs and mysql?

I am trying to figure out what part is wrong? I get this error:

ERROR----- var pwdCompare = await bcrypt.compare(password, pwdCompare[0].password); TypeError: Cannot read properties of undefined (reading '0') ERROR-----

Registering a user is fine. I also hash the password in the user registration... I think i have syntax error or missing code... I just want to figure this part out before I move on to creating a session and a webtoken. ''' router.post('/login', (req, res) => {

var email = req.body.email;
var password = req.body.password;

db.query('SELECT clientID, email, password FROM registration WHERE email = ? AND password = ?', [email, password], async (err, data) => {
    
    

    if (err) {
        throw err;
        
    }
    else if (data[0].password.length == 0) {
        res.locals.messages = req.flash();
        req.flash('info', 'User not found');
        return res.render('login');
    }
    
    
    else {

        let pwd = data[0].password.length;
        
        if (await bcrypt.compare(password,pwd)) {
            res.locals.messages = req.flash();
            req.flash('info', 'Login Successful');
            return res.render('dashboard'); 
        }

        else {
            res.locals.messages = req.flash();
            req.flash('info', 'Login Unsuccessful');
            return res.render('login');
        }
    }
});

'''

ok so after reading the first answer i changed the code and now i get this error ERROR____ var pwdCompare = await bcrypt.compare(password, data[0].length); ^ TypeError: Cannot read properties of undefined (reading 'length') ERROR___

3rd revision so i modified the code again with two parameters in the method and also modified the part you mentioned, but now i am getting this error:

ERROR___ var pwdCompare = await bcrypt.compare(password, data[0].password.length); ^

TypeError: Cannot read properties of undefined (reading 'password') ERROR___

4th Revision I modified the code slightly and i still get the same error. ERROR___ else if (data[0].password.length == 0) { TypeError: Cannot read properties of undefined (reading 'password') ERROR___

I cant figure it out.. i dont understand why it cant read it..



Solution 1:[1]

pwdCompare is undefined at compare method's second parameter.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 SeongJaeSong