I am trying to check if the UID exists in db. If it exist I want it not to write a data else write a data.
But for some reason I am getting exist when there is no such UID.
async function engagements(){
// const db = ;
if (await Engagements.find({uid:"f60c7612-93f5-48b6-8039-94cf5668f6b4"})) {
console.log("exist");
}else{
console.log("does not exist");
}
}
I found the solution. The case that .find() returns an empty array which is equal to true. Basically I made a const and checked the array if it is empty or not
const db = await Engagements.find({uid:"f60c7612-93f5-48b6-8039-94cf5668f6b4"});
if (db.length > 0) {
console.log("exist");
}else{
console.log("does not exist");
}