supabase-js .single() returning array
See original GitHub issueBug report
Describe the bug
After updating to supabase-js v2 rc-1, appending .single()
to a query continues to return an array in the actual data
. The types from the new type gen properly infer that a single non-array item should be returned when .single()
is used, so manually specifying data[0]
produces an any
type inference. However, logging data
shows that it was returned as an array. The same code works as expected in the latest supabase-js v1.
To Reproduce
const { data, error } = await supabase
.from('my_table')
.select(
`
*,
my_relation (
*
)
`
)
.eq('id', itemId)
.single();
// `data` is inferenced correctly as a single `my_table` item
// Additionally, error is null, so .single() did not throw, since there was 1 item returned
// however, data itself is logged as an array of length 1 of the item
// `[{ /* ...itemContents */ }]`
console.log(data);
Expected behavior
.single()
should cause data to return the single object (or throw if multiple) not an array of the object (as inferred by the types).
System information
- OS: Windows
- Browser chrome
- Version of supabase-js: 2.0.0-rc.1
- Version of Node.js: 16.14.2
Issue Analytics
- State:
- Created a year ago
- Comments:7 (4 by maintainers)
Top Results From Across the Web
Working With Arrays - Supabase
Insert a record with an array value#. Dashboard SQL JavaScript. Go to the Table editor page in the ...
Read more >supabase in Next JS returning an empty array when data is in ...
It seems I am always returning an empty array when trying to pull data from my tables. This is how I am doing...
Read more >SELECT statements returns empty array even though data exists
When I perform a select(*) through the js-client , it returns data: ... with series_follows", cardinality: "one-to-many"} Array Prototyp ...
Read more >Query Data From Supabase Using Next.js | egghead.io
js that it can pre-render this at build time by exporting out a getStaticProps() function. Here, we can use the Supabase client to...
Read more >Create function that return table - Supabase SQL
Set of functions to perform calculation on a set of values. The return is a single summary value. (Except for "round()") ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Wow, can’t believe I haven’t run into this particular javascript idiosyncrasy until now! Thanks @soedirgo!!
I adjusted my code and everything works exactly as expected:
Sorry for the late reply - had a look at the repro link (thanks again!) and it seems like this was caused by
init.headers
being a Headers object instead of a{ [key: string]: string }
. You can get the actual values if you do:That said, we don’t make any guarantee that this will always be
Headers
, so you’ll need to handle both cases.