Database error with local edge functions
Hi there,
I'm kind of new to all this, so apologies if there are things that are super obvious to a proper SWE.
I have a project that uses edge functions to store information in my supabase database and storage. That works as intended in my production setting. However, when I try to do this locally, it fails.
What I've done and tested:
- I adjusted the .env variables accordingly and checked with logging that this is correctly setup
- when using a curl request, I actually can connect to my local database as intended
- the local database has the exact same schema as the remote database
- I run npm run dev & supabase functions serve --env-file ./supabase/functions/.env --no-verify-jwt --debug | cat
- When I run my edge function, that is supposed to insert something into a database, I get this error:
[Error] Database error: {
message: "TypeError: error sending request for url (http://localhost:54321/rest/v1/image_generations?select=*)",
details: "TypeError: error sending request for url (http://localhost:54321/rest/v1/image_generations?select=*)\n" +
" at async mainFetch (ext:deno_fetch/26_fetch.js:170:12)\n" +
" at async fetch (ext:deno_fetch/26_fetch.js:391:7)\n" +
" at async Server.<anonymous> (file:///Users/marioottmann/Coding/magicdoodles/supabase/functions/store-generation-metadata/index.ts:36:29)\n" +
" at async #respond (https://deno.land/std@0.208.0/http/server.ts:224:18)",
hint: "",
code: ""
}
I test via both frontend and: curl -i --location --request POST 'http://127.0.0.1:54321/functions/v1/store-generation-metadata' --header 'Content-Type: application/json' --data-raw '{"userId": "00000000-0000-0000-0000-000000000000","preprompt": "test preprompt","prompt": "test prompt","imageUrl": "https://test-image-url.com/image.png"}' | cat
8 Replies
Just run
npm run dev
without all the other stuff as edge functions are served by default once you call npx supabase start
.Hi silentworks, thank you - I tried that but unfortunately, it still fails 😕
also replacing localhost with host.docker.internal did not help.
Wait wait, share your edge function code please
More-so the part where you create the supabase client
// Check environment variables
const supabaseUrl = Deno.env.get('PROJECT_URL')
const serviceRoleKey = Deno.env.get('SERVICE_ROLE_KEY')
console.log('Environment variables status:', { hasSupabaseUrl: !!supabaseUrl, hasServiceRoleKey: !!serviceRoleKey, supabaseUrlValue: supabaseUrl ?
if (!supabaseUrl || !serviceRoleKey) { console.error('Missing environment variables:', { hasUrl: !!supabaseUrl, hasKey: !!serviceRoleKey }) throw new Error('Missing required environment variables') }
// Create Supabase client with the full URL console.log('Creating Supabase client with URL:', supabaseUrl)
const supabaseClient = createClient( supabaseUrl, serviceRoleKey, { auth: { persistSession: false, } } ) and I do import { createClient } from "@supabase/supabase-js"
console.log('Environment variables status:', { hasSupabaseUrl: !!supabaseUrl, hasServiceRoleKey: !!serviceRoleKey, supabaseUrlValue: supabaseUrl ?
${supabaseUrl.substring(0, 8)}...
: undefined // Log first 8 chars for debugging
})
if (!supabaseUrl || !serviceRoleKey) { console.error('Missing environment variables:', { hasUrl: !!supabaseUrl, hasKey: !!serviceRoleKey }) throw new Error('Missing required environment variables') }
// Create Supabase client with the full URL console.log('Creating Supabase client with URL:', supabaseUrl)
const supabaseClient = createClient( supabaseUrl, serviceRoleKey, { auth: { persistSession: false, } } ) and I do import { createClient } from "@supabase/supabase-js"
Yeah these lines are wrong, you don't set these to your own env variables, they are predefined already.
https://supabase.com/docs/guides/functions/secrets#default-secrets
So I do have an .env file in my functions folder and thought this is done via: supabase functions serve --env-file ./supabase/functions/.env
because I think otherwise I got an error because I couldn't name variables starting with SUPABASE_
You cannot set any env variable starting with
SUPABASE_
prefix for edge runtime, these are already predefined and don't need to be set again by you the user. In other words they should not be in your ./supabase/functions/.env
file as they are already setup by edge runtime internally.oh my god! Thank you thank you thank you!!!!!!! it works... I feel so relieved xD
I thought I was going mad