I'm getting an undefined field trying yo compare an user password
this is the function
logUser = (req,res) => {
User.findOne({ username:req.body.username}, (err,user) => {
if(!user){
req.flash('error','El nombre de usuario no existe');
res.redirect('/login');
}else if(!user.comparePassword(req.body.password)){
req.flash('error','La contraseƱa no es correcta');
res.redirect('/login');
}else{
res.redirect('/');
}
});
}
when the execution goes to the else if and uses comparePassword
function the schema field is undefined
the schema file
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const bcrypt = require('bcrypt');
const userSchema = Schema({
username:String,
email:String,
password:String,
registrationDate: {type:Date, defaul: Date.now()}
});
userSchema.methods.encryptPassword = (password) =>{
return bcrypt.hashSync(password, bcrypt.genSaltSync(10));
}
userSchema.methods.comparePassword = (password) =>{
console.log('password: ',this.password);
return bcrypt.compareSync(password, this.password);//here this.password is undefined
}
module.exports = mongoose.model('User',userSchema)
Debugging I saw that user
object is not the schem class, what is worng with tuis class?