After fetching records from Mongo DB, comparePassword method is called to compare the password entered by user and the password stored in database. On printing the record all the data is shown, while inside comparePassword this.password is coming as undefined.
User.findOne ({ username : req.body.username },
function(err, user) {
if (err) throw err;
console.log(user);
user.comparePassword(req.body.password, function(err, isMatch) {
if (err) throw err;
console.log('Password Match:', isMatch);
});});
Method:
UserCredentialSchema.methods.comparePassword = function(pwd, cb) {
bcrypt.compare(pwd, this.password, function(err, isMatch) {
console.log(this.password);
if (err) return cb(err);
cb(null, isMatch);
});};