스터디/Etc

[Supabase] js query예제 모음, 에러 메세지 모음.

Dalmangyi 2023. 12. 21.

supabase가 너무 편하지만 document에 안나오는 방식이 많아서

누군가에게 도움이 될까해서 올립니다.

 

 

 

 

1. foreign table의 내용에서 글자를 찾을때

...

.select(`      
    id,
    rank_idx,
    followers_count,
    creator:Creators!inner(
        user_id,
        user_name,
    ) 
`)   
.or(`or(user_name.like.%${searchText}%, user_id.like.%${searchText}%)`, { foreignTable: 'Creators' })

...

select함수 호출 후에

외래테이블의 값을 찾을땐, options로 {foreignTable:'TABLE_NAME'}을 넣어줘야함.

 

.or로 찾을때는 기본 1개밖에 못 찾음.

2개 이상하려면 계속 중첩적인 or 함수를 이용해야함.

그래서 or함수안에 다시 or함수를 텍스트 형태로 넣음.

 

텍스트 찾을땐 like를 써야하고

앞뒤로 똑같은 어떠한 글자도 상관없이 찾을때는 앞뒤로 %를 붙어줌.

 

 

 

 

 

 

 

2. Or 검색

const { data, error } = await supabase
  .from('cities')
  .select('name, country_id')
  .or('id.eq.20,id.eq.30')

 

 

Or 와 And 검색

const { data, error } = await supabase
  .from('cities')
  .select('name, country_id')
  .or('id.gt.20,and(name.eq.New Zealand,name.eq.France)')

 

배열안에 요소 검색

.or('id.in.(6,7), arraycol.cs.{"a","b"}')  // Use Postgres list () for in filter. Array {} for array column and 'cs' for contains.
.or(`id.in.(${arrList}),arraycol.cs.{${arr}}`)	// You can insert a javascipt array for list or array on array column.
.or(`id.in.(${arrList}),rangecol.cs.[${arrRange})`)	// You can insert a javascipt array for list or range on a range column.

 

 

 

 

 

 

 

 

 

 

 


 

에러 메세지 모음.

 

1. single() 함수를 사용할때 자주 겪는 에러.

.select(`    
    id,
    created_at,
`) 
.limit(1) 
.single()

이렇게 간단한 코드임에도 에러가 나는 경우가 있다.

 

에러메세지

JSON object requested, multiple (or no) rows returned

객체를 반환 요청했지만, 여러행이거나 행이 없이 리턴 됬다는 말입니다.

single()함수는 select로 반환되는 배열이 1개일때, 배열로 반환되지 않고 객체로 반환될 수있게 도와주는 함수입니다.

select()함수가 여러개의 배열로 반환되고, limit(1)함수로 1개로 한정 지었지만 single에서는 에러가 날 수있습니다.

이유는 select()함수를 사용 했을때 값이 아예 없으면, limit(1) 함수를 실행해봤자, 값이 없고, 이걸 객체로 변환할려니 발생하는 에러입니다.

 

왠만하면 객체로 받고 싶으면, limit()함수와 maybeSingle()함수를 사용하시길 바랍니다.

.select(`    
    id,
    created_at,
`) 
.limit(1) 
.maybeSingle()

 

 

 

 

 

 

 

 

 

 

 

댓글