I'm new to Mongoose and also using async/await. Right now I have a Mongoose schema, with a static method, like so:
const userSchema = new Schema({
username: String,
pass: String
});
userSchema.statics.checkExist = async function(username){
return await this.findOne({username: username}, function(err, res){
if(err){
return err;
}
if(res){
return true;
}
else{
return false;
}
})
}
The static method checkExist() is supposed to take in a username, and check if a document already exists with the same username. If it does, it should return true, otherwise false. I use it in my NodeJS/Express server like so:
router.post('/auth/login', async (req, res) =>{
let username = req.body.username;
let existBool = await UserModel.checkExist(username);
console.log(existBool);
res.send({'hash': existBool});
});
I expect existBool to be the true/false boolean. Instead, checkExist() doesn't seem to call the callback function at all. Instead it returns the result of findOne(), which is an object that has the matching username field. What am I doing wrong here?