Introduction
When retrieving data with Superbase,
Have you ever encountered the problem of getting an array even though you only retrieved one record?
This happened to me the first time, and data.name became undefined, which was a problem for me.
In this article, I'll show you how to retrieve just one record as an object.
Problem Code
supabase.from("study_spots").select("*").eq("id", id);
Results
[ { id: 1, name: "Cafe A" } ]
Even a single record is returned as an array ([ {...} ]).
Therefore, accessing data.name returns undefined.
Solution
If you want only one record, add .single().
supabase.from("study_spots").select("*").eq("id", id).single();
This returns the result.
{ id: 1, name: "Cafe A" }
→ Returns an object, not an array.
Summary
Supabase returns an array by default.
Adding .single() returns a single record.
Top comments (0)