I am developing nodejs API. In some of my APIs, i need to check whether some of the parameters are unique or not in database and i need to code it in a separated method to share the function. (PS: I don't want to do it in sequelize models)
My api call like this
var duplicate = checkUniqueEmail(req.body.email);
console.log(duplicate);
if(duplicate == true){
//do something
} else {
//return error
};
The checking method like this
function checkUniqueEmail(email){
db.users.find( {
where: {email: email}
}).then( (result) => {
if(result !== null){
return false;
} else {
return true;
}
}).catch(err => {
console.log(err);
});
}
But finally i got undefined
by console.log(duplicate)
The problem seems that i log the result before it executing and return some results,it should be a javascript async problem.
How can i modify my code to achieve my target?