I have no idea why I cannot hide the promise correctly, here is my code
app.post('/login', urlencodedParser, async (req, res) => {
model.authenticate(req.body.uname, req.body.pword)
.then(function (result) {
console.log(result);
});
});
async function authenticate(uname, pword) {
User.find({username: uname}, 'password', function (err, result) {
if (err !== null){
return new Promise(function (resolve, reject) {
resolve(false);
});
}
else{
if (pword === result[0].password){
console.log("Correct Password!");
return new Promise(function (resolve, reject) {
resolve(true);
});
}
But the output is in my console
undefined
Correct Password!
which indicate that .then () is implemented before authentication is complete. So how can I code it better? many thanks!
source
share