DEV Community

Kazutora Hattori
Kazutora Hattori

Posted on

How to retrieve only one record in Supabase (.single())

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);
Enter fullscreen mode Exit fullscreen mode

Results

[ { id: 1, name: "Cafe A" } ]
Enter fullscreen mode Exit fullscreen mode

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();
Enter fullscreen mode Exit fullscreen mode

This returns the result.

{ id: 1, name: "Cafe A" }
Enter fullscreen mode Exit fullscreen mode

→ Returns an object, not an array.

Summary

Supabase returns an array by default.
Adding .single() returns a single record.

Top comments (0)