How do I use the `exists` function like `select ex...
# help
j
How do I use the
exists
function like
select exists(select 1 from contact where id=12)
in supabase?
n
Hello @jdgamble555! This thread has been automatically created from your message in #843999948717555735 a ``few seconds ago``. Pinging @User so that they see this as well! Want to unsubscribe from this thread? Right-click the thread in Discord (or use the ... menu) and select Leave Thread to unsubscribe from future updates. Want to change the title? Use the
/title
command! We have solved your problem? Click the button below to archive it.
g
Can you give a bit more detail on what you are asking? What do you mean by supabase? The js client?
n
jdgamble555 (2022-04-03)
j
This does not work for example:
Copy code
typescript
const { data } = await supabase.from('profiles').select(`exists( username )`).eq('id', user.id)
In postgres, I should be able to return true or false is something exists...
g
exists is only on the postgres side. On the JS side the closest to exists would be you get an array with selected data. So data.length...
If you don't want data returned I guess you could use select with a count only returned
j
Ok... odd. When I query something that doesn't exist, I get this error in console.log:
Copy code
GET https://22234fdsa.supabase.co/rest/v1/profiles?select=username&id=eq.7b8bca86-47ad-4928-8f56-538638e09774 400
which I would like to get rid of.
b
Well you would obviously have to catch the error and do something with it
const { data, error } =
If error do stuff
j
right, but it prints to the console even when I do that...
I don't have console.log anywhere in my code
b
No big website I know of have a clean console
And to my knowledge no user cares
But
Yh idk
j
Hmm. IMHO that is bad coding, as errors should be handled correctly.
g
What is your exact call you get 400 on? If you request .eq and it does not match you should get empty array
Nah I’m kidding
But I honestly have no clue how to disable this
Maybe Gary does
j
I am querying a user record that does not exist. I would like to not print to the console and check if the user exist.
g
Please show your query it should return []
j
Copy code
typescript
        const { data, error, status } = await supabase
            .from('profiles')
            .select(`username`)
            .eq('id', user.id)
            .single();
It does return [], but it still prints to the console
g
drop .single
That means you MUST have one and only 1. 0 is an error, 2 or more is an error.
b
Id is unique
g
You can use .maybeSingle() if you are sure it will never be more than one. Then 0 or 1 result works.
j
the single fixed the problem. Thank you sir!
n
Thread was archived by @jdgamble555. Anyone can send a message to unarchive it.