supabase / postgrest-js

Isomorphic JavaScript client for PostgREST.
https://supabase.com
MIT License
967 stars 130 forks source link

Getting 406s in network logs if `.maybeSingle()` returns no result #361

Closed probablykasper closed 8 months ago

probablykasper commented 1 year ago

Bug report

Describe the bug

When selecting using .maybeSingle(), I'm getting an error logged to console if there's 0 rows returned. There's no error if I remove .maybeSingle(), and there's no error if a row is returned.

To be clear, the query still returns successfully.

image

The stack trace seems to points to this line (probably a bad source map): https://github.com/supabase/postgrest-js/blob/84f151834a4e305508cd246cba9ea33e6affc0f2/src/PostgrestBuilder.ts#L102

To Reproduce

    const profileResult = await supabaseClient
        .from('profiles')
        .select()
        .eq('id', session.user.id)
        .maybeSingle()

System information

soedirgo commented 1 year ago

The stack trace seems to points to this line (probably a bad source map)

Hmm the trace points to PostgrestBuilder.ts:70 which looks right:

https://github.com/supabase/postgrest-js/blob/84f151834a4e305508cd246cba9ea33e6affc0f2/src/PostgrestBuilder.ts#L70

So it comes from the fetch invocation. I suspect the browser's native fetch is logging all 4xxs - the way we do maybeSingle() is we do single() but ignore the error, so the 406 comes from PostgREST. Should be harmless, but noisy - I suppose we could instead treat it as a non-single() and just throw if there are >1 rows.

steve-chavez commented 1 year ago

the way we do maybeSingle() is we do single() but ignore the error, so the 406 comes from PostgREST

Not ideal to ignore the error on the client as the logs would also still contain the 406. I think https://github.com/PostgREST/postgrest/pull/2164#issuecomment-1165035270 would be the ideal solution.

(basically a header that defines expected number of returned rows)

soedirgo commented 1 year ago

Gotcha, changing this issue to track refactoring the code to use that feature once it's out :+1:

brentrobbins commented 1 year ago

Hi @soedirgo Any update on when this might be addressed?

soedirgo commented 1 year ago

@brentrobbins can try to npm update @supabase/postgrest-js? This should fix most of the cases, the rest will need some changes on PostgREST proper

otang commented 11 months ago

I am using @supabase/postgrest-js@1.7.2 and I'm still having the issue where the API returns 406 errors when using .maybeSingle().

This seems to be a mistake. I would expect this type of query to return a 200 status with a null result, as opposed to a non-200 status.

francisace commented 8 months ago

Any update on this? For us the issue appears to be this line: https://github.com/supabase/postgrest-js/blob/v0.37.4/src/lib/types.ts#L151 which contains hard coded Results contain 0 rows, currently the endpoint returns The result contains 0 rows in the details

steve-chavez commented 8 months ago

On PostgREST 11.2.0, the format of the error got changed on https://github.com/PostgREST/postgrest/pull/2876 to "The result contains 0 rows".

I didn't know postgrest-js was relying on the messages. For now we could change the detection to 0 rows. This should work for previous and newer versions.

github-actions[bot] commented 8 months ago

:tada: This issue has been resolved in version 1.8.5 :tada:

The release is available on:

Your semantic-release bot :package::rocket: