I’m new in express nodejs and using MongoDB 7. So i get several error when using the below code: (which is route/users.js)
passport.use(new LocalStrategy({ passReqToCallback : true},
async function(username, password, done) {
await User.getUserByUsername({ username: username }, function(err, user){
if(err) throw err;
if(!user){
console.log('Unknow User');
return done(null, false, { message: 'Incorrect username or password.' });
}
User.comparePassword(password, user.password, function(err, isMatch){
if(err) throw err;
if(isMatch){
return done(null, user);
}else{
console.log('Invalid Password');
return done(null, false, {message: 'Invalid Password'});
}
});
});
}
));
and next model/user.js have the below code:
> module.exports.comparePassword = function(candidatePassword, hash, callback){
bcrypt.compareSync(comparePassword, hash, function(err, isMatch){
if(err) return callback(err);
callback(null, isMatch);
});
}
module.exports.getUserById = function(id, callback){
User.findById(id, callback);
}
module.exports.getUserByUsername = function(username, callback){
var query = {username: username};
User.findOne(query,callback);
}
Please if possible give the total solution above code.Thx
TypeError: Model.findOne() have error no longer call back accepted