question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

supabase-js .single() returning array

See original GitHub issue

Bug 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:closed
  • Created a year ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
Daynilcommented, Sep 1, 2022

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:

export async function supabaseFetch(
  input: RequestInfo | URL,
  init?: RequestInit
) {
    if (init.headers instanceof Headers) {
        init.headers.set('Authorization', `Bearer ${await getSupabaseToken()}`);
    } else {
        init.headers['Authorization'] = `Bearer ${await getSupabaseToken()}`;
    }
    return fetch(input, init);
}
1reaction
soedirgocommented, Sep 1, 2022

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:

for (const [headerKey, headerValue] of init?.headers?.entries() ?? []) {
  console.log('header:', headerKey, headerValue)
}

That said, we don’t make any guarantee that this will always be Headers, so you’ll need to handle both cases.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found