<aside> ✍️ 작성자: 김혜원 작성일: 24.06.12

</aside>

문제

**const** { data, error } = await supabase.from("class").select("*");

위와 같은 코드로 Supabase DB의 데이터를 가져오고자 할 때,

{ error: null, data: [], count: null, status: 200, statusText: 'OK' }

이렇게 error 없고, status도 정상인데 data가 빈 배열로 마치 테이블에 데이터가 없는 것처럼 반환된다.

Supabase SQL Editor로 query하면 아래와 같이 5개 데이터가 출력된다.

Screenshot 2024-06-12 at 10.12.49 AM.png

프론트에서 위 코드로 DB를 불러올 때도 이처럼 5개 데이터가 담긴 배열이 반환되어야 한다.

원인

이번 에러는 에러메시지가 없고, 정상 작동되었다고만 떠서 원인과 해결방법을 찾기 어려웠다. 문서와 코드를 아무리 봐도 잘못된 부분이 없었다. 결국 검색을 하던 중 아래의 글을 발견했다.

https://stackoverflow.com/questions/75039258/supabase-returns-empty-array

내가 겪는 문제와 완전히 일치한 상황이었다. 답변을 보니 RLS 때문이었다.

(위 링크 StackOverflow 답변) RLS stands for Row Level Security, it makes it so that only the owner of the row can view their own data. Let's say user A inserts a line of data, only user A can see it. If you run select * as user B, you won't be able to see it. This helps prevent other users from interacting with a user's items.

이걸 안 후에 보니, Supabase에도 Enable RLS 체크박스 밑에 이런 경고문이 있었다.

Policies are required to query data You need to create an access policy before you can query data from this table. Without a policy, querying this table will return an empty array of results.