The mysql query always returns undefined and I don't know whats wrongs. It used to work before I added the async await to the functions but that used to return an error in the terminal because bcrypt is an async function. Could someone help me with this? Here is my code
app.post('/login', async function(req, res) {
let username = req.body.username;
let password = req.body.password;
if (username && password) {
connection.query('SELECT username, password, email FROM accounts WHERE username = ?', [username], async function(err, result, fields) {
if (result.length > 0) {
try {
if(await bcrypt.compare(password, result.password)) {
req.session.loggedin = true;
req.session.username = username;
res.redirect('/');
}
else {
res.send('Incorrect Username and/or Password!');
}
}
catch (err) {
//res.status(500).send();
res.send('Something wrong happened');
console.log(err);
}
} else {
res.send('Incorrect Username and/or Password!');
}
res.end();
});
} else {
res.send('Please enter Username and Password!');
res.end();
}
});