#How do i check if data already exists in mongodb before writing it?
85 messages · Page 1 of 1 (latest)
well the first thing i can see there is that your guard clause isn't awaited
idk if that's an issue personally since i'm not all that on mongoose fyi, but based on your code a little further down i'm guessing it's async
since you're using find as opposed to findOne the return type should be array-like
if it's array-like, then if something isn't found, it'll be returning an empty array
and js being js, an empty array is truthy
try changing it to findOne and test whether it's === null
(truthy is a problem for you since even if mongoose doesn't find any matching User documents, it will still evaluate to true)
Whats a gaurd clause?
I need it to return true or false. Is that a problem?
it's what you've done. alternative to if/else, you're checking if something is true and then returning to exit the function
Ill change it to findOne
if (User.find[...]) but you probably want to await the result of the find / findOne
No i didnt
tbh im not very good at using async and await
ill try to set it up and ill show you what i did
pls copy and paste instead of screenshots when sharing code
Alright ill do that
it's hard to help if i can't quote parts
// before adding to the database, see if it already exists
if (await User.findOne( { name: name} )){
console.log("not adding to the database")
return;
}
const user = await User.create({
name: name,
email: email
})
// user.name = "Dajuan"
await user.save()
console.log(user);
}
I did it
im gonna test it
Wait that worked
Yeah i do
It runs the function but skips over it
so await makes it stay and wait for a result
sorta. it was actually not skipping over it, but the opposite, but for a related reason
basically a promise is an object that eventually becomes a value right
I dont fully understand asyncronous code but sure
Im trying to learn it
well, if you say if ({}) then you get true
Yeah
because it's an empty object, and according to js, empty objects == true
a promise is an object, so it's always == true
Yeah
so your code before was saying if (mongodbstuff)
mhmm
but because you weren't awaiting, it was evaluating the object it had - which was a promise - which was true. so it was always entering the block
Ohhh
by using await, like you said, it waits for the final value before checking whether it's true or false
Ohh yeah
the other half was with the other change we talked about, findOne
before when you were using find, because find can find multiple records, it returns them in an array-like structure
so even if you awaited it, if you used find, then the empty array which got returned would have still == true because in js empty arrays == true
Yeah your right
two separate problems which arrived in the same bug basically
np
Do you know how to use compass?
alas nope
no no i said alas lol
Do you know anyone who could help me with mongoose and compass?
i couldn't tell that english wasn't your first language haha
afraid not probably worth a separate thread
also be specific about what your problem is since it might be a generic-enough problem that general knwoeldge can solve it
Im from the US so it is lmao
Yeah
im new to this
Mongodb is just confusing
y'all don't say alas? maybe a uk thing then
Yeah its def a UK thing
Ill create a new thread for it
im gonna delete this
tysm tho
don't delete it
Oh okay i wont
leave it in case someone has a similar problem