I am using the MERN stack, I have a problem where mongoose findOne returns first object found even if condition is not true This is my user model
{ local:{
username,
email,
password
},
google: {
googleId,
email,
name
}
}
I have this schema statics that find if email exists or not
userSchema.statics.findByEmail = async (email) => {
const localUser = await User.findOne({ "local.email": email });
const googleUser = await User.findOne({ "goolge.email": email });
return { localUser, googleUser };
};
In my api before i add a user i search for it in my database using findByEmail
router.post("/register", async (req, res) => {
const user = await User.findByEmail(req.user.email);
})
if there is no documents in my database, the api works perfectly but if there is at least one document, it always returns first document in my database even if req.user.email
isn't in my database
CodePudding user response:
This problem happens in case in case the field is not declared in the schema where its being queried.
Please make sure that in your User Schema these fields exists: "local.email" and "goolge.email"
