i wanna check if user is existing in the database but I got this error when I add results.length:
(node:12124) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'length' of undefined
This is the code:
const mysql = require('mysql')
const jwt = require('jsonwebtoken');
const bcrypt = require('bcryptjs');
exports.register = (req, res) => {
console.log(req.body);
const { name, email, password, passwordConfirm } = req.body;
db.query('SELECT email FROM users WHERE email = ?', [email], async (error, results) => {
if (error) {
console.log(error);
}
if (results.length > 0) {
return res.render('register', {
message: 'That email is already in use'
})
} else if (password !== passwordConfirm) {
return res.render('register', {
message: 'Passwords do not match'
});
}
let hashedPassword = await bcrypt.hash(password, 8);
console.log(hashedPassword);
});
}
That seems pretty straightforward... If the user doesn't exist, results is undefined, so your condition should not check for results.length but instead:
if (results !== undefined) {
return res.render('register', {
message: 'That email is already in use'
})
}