I want to do a password verification using express and mongoose.
All right until the part where I check the password, I just can not access that value of my document.
This is my function:
function iniciar_sesion(req, res)
{
let usu = new Usuario({
usuario : req.body.usuario,
password : req.body.password
})
Usuario.findOne( {usu_nombre: usu.usuario}, (err, user) => {
// Comprobar si hay errores
if (err) return res.status(500).send({message: 'Error al realizar la petición: ${err}'})
// Comprobar si el usuario existe
if (!user) return res.status(404).send({message: 'No existe el usuario '})
// Comprobar si la contraseña es correcta
console.log(user._id) // marca su valor
console.log(user.usu_contrasenia) // marca undefined
console.log(user.usu_nombre) // marca undefined
console.log(user)
if (user.usu_contrasenia != usu.password)
return res.status(400).send({message: 'Contraseña incorrecta'})
// Genero token
res.status(200).send(user)
})
}
However, when I do the console.log (user) it prints the entire document:
{ _id: 586151481787e016382ee4b2,
usu_nombre: 'John Doe',
usu_contrasenia: '123' }
You should not be able to sign in by doing: user.usu_contrasenia ????????
The funny thing is that the _id does print it and it does correspond to the DB.
Any help?
Note: For now I am driving without encrypting the password, I wanted to do this test first but I do not get it o.O