Hello everybody, I’m doing a login authentication with mongoose and I’m trying to implement a function to check if that username is taken already.
This is my function.
const checkFields = username => {
userSchema.findOne({ "username": username })
.then(user => {
return user !== null ? true : false
})
}
I need to return a true or false from that function to this one
else if (checkFields(req.body.username)) {
res.render("signup", {
errorMessage: "The username already exists!"
})
I tried differents ways but I coudn’t success in any of them. CheckFields is returning undefined because i’m not returning anything. I tried to make a variable and assign to true or false inside of then but I lost the value when I do a return.
I also tried this
const checkFields = username => {
return userSchema.findOne({ "username": username })
.then(user => {
return user !== null ? true : false
})
}
This is the output : Promise { <pending> }
so my condition is true all the time.
Could you give me a hand?
Thanks in advanced