Hello guys any idea why i need to json.parse the o...
# help
m
Hello guys any idea why i need to json.parse the output of one of my table ?
Copy code
const { data, error } = await supabase
          .from<definitions['users']>('users')
          .select()
          .eq('id', auth?.id)
          .limit(1)
          .single()
        if (!error && data) {
          console.log('user', data)
          main.user = JSON.parse(data as any as string) // TODO: Understand why it's needed
          // main.user = data
        }
n
Hello @Martin INDIE MAKERS! This thread has been automatically created from your message in #843999948717555735 a few seconds ago. We have already mentioned the @User so that they can see your message and help you as soon as possible! Want to unsubscribe from this thread? Right-click the thread in Discord (or use the ``...`` menu) and select "Leave Thread" to unsubscribe from future updates. Want to change the title? Use the ``/title`` command! We have solved your problem? Click the button below to archive it.
m
if i remove the single and take the
[0]
element i don't have this issue
g
Something to do with Typescript maybe?
Copy code
const {data} = await supabase
        .from('messages')
        .select('*')
        .eq('id',6)
        .single()
    const row = data
    console.log('select',data,data.id,row.id)
Works fine.
m
@garyaustin nope there no issue with typescript and that the only table where this happen if i do the same i you get that
the data coming from the SDK is not in the right format
g
Hmmm, my response What is your accept header? accept: application/vnd.pgrst.object+json
m
@garyaustin ok i think it's because i have a custom fetch implementation. for my app i use http native plugin
for capacitor app i use https://github.com/capacitor-community/http to remove the need of option call
Copy code
const options: SupabaseClientOptions = {
    autoRefreshToken: true,
    persistSession: true,
    detectSessionInUrl: false,
    fetch: (requestInfo, requestInit) => {
      const url = requestInfo.toString()
      if (requestInit?.method === 'POST' && (url.includes('/storage/') || url.includes('/rpc/'))) {
        return fetch(requestInfo, {
          method: requestInit?.method,
          signal: requestInit?.signal || undefined,
          headers: requestInit?.headers,
          body: requestInit?.body,
        })
      }
      return Http.request({
        url,
        method: requestInit?.method,
        headers: requestInit?.headers as any || {},
        data: requestInit?.body,
      })
        .then((data) => {
          const resp = new Response(JSON.stringify(data.data), {
            status: data.status,
            headers: data.headers,
          })
          return resp
        })
    },
  }
but as you can see it seems there some exception hard to handle
the issue here is the
JSON.stringify
who is necessary for all call, but fail when i do
single()
Same for storage and rpc i cannot use this module
to give you context this module use fetch in web (where i try now) and native http in ios and android
so i fixed it by doing
Copy code
const type = data.headers['content-type']
          const res = type.includes('application/vnd.pgrst.object+json') ? data.data : JSON.stringify(data.data)
          const resp = new Response(res, {
            status: data.status,
            headers: data.headers,
          })
that also fix the
rpc
issue, but not the storage who is normal the bug is in the http plugin
for the people who have the same issue, my current options
Copy code
const options: SupabaseClientOptions = {
    autoRefreshToken: true,
    persistSession: true,
    detectSessionInUrl: false,
    fetch: (requestInfo, requestInit) => {
      const url = requestInfo.toString()
      if (requestInit?.method === 'POST' && url.includes('/storage/')) {
        return fetch(requestInfo, requestInit)
      }
      return Http.request({
        url,
        method: requestInit?.method,
        headers: requestInit?.headers as any || {},
        data: requestInit?.body,
      })
        .then((data) => {
          const type = data.headers['content-type']
          const res = type.includes('application/vnd.pgrst.object+json') ? data.data : JSON.stringify(data.data)
          const resp = new Response(res, {
            status: data.status,
            headers: data.headers,
          })
          return resp
        })
    },
  }
The related issue for http module
n
Thread was archived by @Martin INDIE MAKERS. Anyone can send a message to unarchive it.