SupabaseS
Supabase4y ago
Ro

JSON parsing issues with Supabase Edge Functions

It's my first time using edge functions from the browser, so I set up the test methods from docs. I can't get past this error. Can anyone help?

error : "\"[object Object]\" is not valid JSON"

I have a function in my browser client code like this:
export const test = async (name) => {
  const { data, error } = await supabaseClient.functions.invoke('hello', {
    body: { foo: name }
  });
  console.log('data: ', data);
  return data?.message;
};


And the edge function:
serve(async (req) => {
  // This is needed if you're planning to invoke your function from a browser.
  if (req.method === 'OPTIONS') {
    return new Response('ok', { headers: corsHeaders })
  }

  try {
    const { body } = await req.json()
    const data = {
      message: `Hello ${JSON.stringify(body)}!`,
    }

    return new Response(JSON.stringify(data), {
      headers: { ...corsHeaders, 'Content-Type': 'application/json' },
      status: 200,
    })
  } catch (error) {
    return new Response(JSON.stringify({ error: error.message }), {
      headers: { ...corsHeaders, 'Content-Type': 'application/json' },
      status: 400,
    })
  }
})
Was this page helpful?