realtime-js icon indicating copy to clipboard operation
realtime-js copied to clipboard

JSON parse error: SyntaxError: JSON Parse error: Unexpected character: d

Open evelant opened this issue 1 year ago • 4 comments

Bug report

  • [X ] I confirm this is a bug with Supabase, not with my own application.
  • [X ] I confirm I have searched the Docs, GitHub Discussions, and Discord.

Describe the bug

supabase-js is logging JSON parse error: SyntaxError: JSON Parse error: Unexpected character: d with no stack and no further detail after successfully upserting a row to the database.

To Reproduce

   const { count, data, error } = await db
                        .from(table_name)
                        .upsert(snapshot as any, { ignoreDuplicates: false, onConflict: "id" })
                        .eq("id", id)
                        .select()
                        .single()

With the above upsert error is null but somewhere internal to supabase-js logs JSON parse error: SyntaxError: JSON Parse error: Unexpected character: d. If I comment out this upsert operation the spurious error log goes away.

Expected behavior

If there is an error supabase-js should return or throw it

Screenshots

N/A

System information

  • OS: macOS
  • Browser (if applies) [e.g. chrome, safari]
  • Version of supabase-js: 2.39.3
  • Version of Node.js: N/A

Additional context

evelant avatar Feb 07 '24 20:02 evelant

Actually this appears to be coming from realtime. The following code:

export function listenToDbDocument<DocType extends { [key: string]: any }>(
    connection: SupabaseClientType,
    table_name: string,
    docId: string,
    onDocChange: (change: DbDocChangeType<DocType>) => void,
): () => void {
    log.log(`Listening to "${table_name}" for document "${docId}"`)

    connection
        .from(table_name)
        .select("*")
        .eq("id", docId)
        .single()
        .then(doc => {
            console.log(`got initial doc`)
        })

    const channel = connection
        .channel(docId)
        .on(
            "postgres_changes",
            { event: "UPDATE", schema: "public", table: table_name, filter: `id=eq.${docId}` },
            update => {
               console.log(`got change`)
            },
        )
        .subscribe()

    return channel.unsubscribe
}

On every change that comes in on the postgres_changes channel it logs JSON parse error: SyntaxError: JSON Parse error: Unexpected character: d just before the callback is run with the update. There are no errors, the update message is fine.

evelant avatar Feb 07 '24 20:02 evelant

Having similar issues here, have a strange LOG JSON parse error: SyntaxError: JSON Parse error: Unexpected end of input

I log all the payload is fine

ddx-510 avatar Jul 04 '24 13:07 ddx-510

Having similar issues here, have a strange LOG JSON parse error: SyntaxError: JSON Parse error: Unexpected end of input

I log all the payload is fine

Hey, I eventually found out that the backend inserted a wrong type to a jsonb field in supabase. However, this does not trigger any error log cuz the payload is already inside and it is assumed to be the correct type.

So either avoid this in the backend or find some ways to raise this error in supabase js

ddx-510 avatar Jul 05 '24 07:07 ddx-510