#How do i check if data already exists in mongodb before writing it?

85 messages · Page 1 of 1 (latest)

teal mist
#

I have a function that adds information to the database. I would like to check if the data already exists and if it does, return the function and not continue. Im not sure whats going on. Every time i run the function it returns and cancels.

icy maple
#

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)

teal mist
icy maple
teal mist
icy maple
#

if (User.find[...]) but you probably want to await the result of the find / findOne

teal mist
#

It still didnt work

#

I also have questions about Compas

icy maple
#

did you await?

#

if it's async then it's returning a promise object, which is truthy

teal mist
#

tbh im not very good at using async and await

#

ill try to set it up and ill show you what i did

icy maple
#

pls copy and paste instead of screenshots when sharing code

teal mist
#

Alright ill do that

icy maple
#

it's hard to help if i can't quote parts

teal mist
#
    // 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);
}
teal mist
#

im gonna test it

#

Wait that worked

icy maple
#

ya

#

do you understand why

teal mist
#

Yeah i do

#

It runs the function but skips over it

#

so await makes it stay and wait for a result

icy maple
#

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

teal mist
#

Im trying to learn it

icy maple
#

well, if you say if ({}) then you get true

teal mist
#

Yeah

icy maple
#

because it's an empty object, and according to js, empty objects == true

#

a promise is an object, so it's always == true

teal mist
#

Yeah

icy maple
#

so your code before was saying if (mongodbstuff)

teal mist
#

mhmm

icy maple
#

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

teal mist
#

Ohhh

icy maple
#

by using await, like you said, it waits for the final value before checking whether it's true or false

teal mist
#

Ohh yeah

icy maple
#

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

icy maple
#

two separate problems which arrived in the same bug basically

teal mist
#

Yeah

#

thank you so much for this

#

i learned a lot

icy maple
#

np

teal mist
#

Do you know how to use compass?

icy maple
#

alas nope

teal mist
#

Not atlas

#

atlas is the cloud thing right?

icy maple
#

no no i said alas lol

teal mist
#

Wait im confused

#

you know how to use atlas?

icy maple
#

noooo alas

#

alas like, unfortunately

teal mist
#

OHHH

#

ive never seen anybody say that

teal mist
icy maple
#

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

teal mist
teal mist
#

im new to this

#

Mongodb is just confusing

icy maple
teal mist
#

Yeah its def a UK thing

#

Ill create a new thread for it

#

im gonna delete this

#

tysm tho

icy maple
#

don't delete it

teal mist
#

Oh okay i wont

icy maple
#

leave it in case someone has a similar problem