I try to define function that gets email as input value and returns the status of user whether its exists or not with this email , but actully I`m not able to return the value via find method from mongoose model which is User here! this is my code :
const User = require('./models/user')
// checking if user with this email exists or not and return true if it exists
function isExists(email) {
return User.findOne({email : email})
.then(user =>{
if(!user){
return false
}
return true
}).catch(err =>{
console.error(err)
})
}
let status = isExists('example@example.com')
console.log(status);
also I tried other way but it didn`t work :
async function getUser(email) {
try {
let user = await User.findOne({email : email})
return user ;
} catch (err) {
return err
}
}
let user = await getUser('example@example.com')
console.log(user);
In all of these approaches I try to return value of find method to other variable but none of them work correctly and I dont know why? I think the main problem is my understanding of logic of promises and async/await