I got 'TypeError: fetch failed'

const supabaseAdmin = createClient( supabaseUrl, supabaseServiceKey, { auth: { persistSession: false, autoRefreshToken: false, }, } ); const { data: testConnection, error: connectionError } = await supabaseAdmin .from('users') .select('id') .limit(1); if (connectionError) { console.error('Supabase connection error:', connectionError); return NextResponse.json( { error: 'Database connection failed. Please ensure Supabase is running.' }, { status: 503 } ); } This is my code. but when I try to run this, I got the error.
message: 'TypeError: fetch failed',
message: 'TypeError: fetch failed',
How can I fix this problem? Any help is thanks. thanks.
30 Replies
garyaustin
garyaustin2mo ago
That is a pretty generic error usually meaning the fetch never made it to Supabase. Check your dashboard API Gateway log to see if there. Otherwise you have to see if it even makes a network call from your code and if so check the URL. Log out your SupabaseUrl to make sure correct. Also you show a message about Database connection failed 503, is that supposed to be the error you are getting or something you made up to return for you API.
david_eason
david_easonOP2mo ago
I defined 503 error. @garyaustin Sorry ping you. Now, I am not sure what you mean. I am getting this error still now. Could you please help me one by one?
garyaustin
garyaustin2mo ago
I gave you some things to check/provide so a user here might help.
Did you console.log your supabaseURL to Supabase right before you create the client? Did you try and look at the dev console from the browser (if that works for your environment as the call to SB might be from a server) for the network request? Are you sure the 503 is to Supabase and not your Next server code? Also is this for all Supabase calls or just this one?
What is the status of your project (main dashboard page for project)?
david_eason
david_easonOP2mo ago
Everything looks good.
david_eason
david_easonOP2mo ago
I got this error when I try to send request.
No description
david_eason
david_easonOP2mo ago
using postman. Could you please help me this?
garyaustin
garyaustin2mo ago
What request did you make?
david_eason
david_easonOP2mo ago
https://xxx.supabase.com/rest/v1/rpc/get_user_by_email
https://xxx.supabase.com/rest/v1/rpc/get_user_by_email
I made get_user_by_email function
garyaustin
garyaustin2mo ago
Can you check the Postgres log for what is getting that error? The error means the user calling something in the public schema does not have a grant on it. This could mean you or an AI if you use one changed the schema grants.
david_eason
david_easonOP2mo ago
here?
No description
david_eason
david_easonOP2mo ago
"connection authenticated: user="supabase_admin" method=trust (/etc/postgresql/pg_hba.conf:85)"
garyaustin
garyaustin2mo ago
Logs then Postgres Then look for an ERROR message Did you change grants on the public schema? Do you use an AI that might have?
david_eason
david_easonOP2mo ago
There is no error. 🙁
No description
garyaustin
garyaustin2mo ago
How long ago did you run the test? You can look back further and you can filter on error up top. The error message you got came from the DB so it will be there or you are looking at a different project.
david_eason
david_easonOP2mo ago
In API Gateway, I saw this error.
david_eason
david_easonOP2mo ago
No description
garyaustin
garyaustin2mo ago
Try to just select a table from postman And you have not answered about grant changes.
david_eason
david_easonOP2mo ago
sorry bro. I am begginer. Could you please explain about grant?
garyaustin
garyaustin2mo ago
You could have run SQL to keep anon/authenticated users from running.
Something like REVOKE ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA "public" FROM "anon"; would keep anon from using public functions with the type of error you are seeing. Please try the table select from postman. Also you need to double check the Postgres logs as they have to have a permission error in them. There is some SQL to run that can fix grants, but sort of need to know if you purposely wanted/needed to remove them for some reason.
david_eason
david_easonOP2mo ago
WITH pgrst_source AS (SELECT "pgrst_call".* FROM (SELECT $1 AS json_data) pgrst_payload, LATERAL (SELECT "user_email" FROM json_to_record(pgrst_payload.json_data) AS _("user_email" text) LIMIT 1) pgrst_body , LATERAL "public"."get_user_by_email"("user_email" := pgrst_body."user_email") pgrst_call) SELECT null::bigint AS total_result_set, pg_catalog.count(_postgrest_t) AS page_total, coalesce(json_agg(_postgrest_t), '[]') AS body, nullif(current_setting('response.headers', true), '') AS response_headers, nullif(current_setting('response.status', true), '') AS response_status, '' AS response_inserted FROM (SELECT "record".* FROM "pgrst_source" AS "record" LIMIT $2 OFFSET $3) _postgrest_t
WITH pgrst_source AS (SELECT "pgrst_call".* FROM (SELECT $1 AS json_data) pgrst_payload, LATERAL (SELECT "user_email" FROM json_to_record(pgrst_payload.json_data) AS _("user_email" text) LIMIT 1) pgrst_body , LATERAL "public"."get_user_by_email"("user_email" := pgrst_body."user_email") pgrst_call) SELECT null::bigint AS total_result_set, pg_catalog.count(_postgrest_t) AS page_total, coalesce(json_agg(_postgrest_t), '[]') AS body, nullif(current_setting('response.headers', true), '') AS response_headers, nullif(current_setting('response.status', true), '') AS response_status, '' AS response_inserted FROM (SELECT "record".* FROM "pgrst_source" AS "record" LIMIT $2 OFFSET $3) _postgrest_t
permission denied for schema public
permission denied for schema public
I found this error message in postgres log
garyaustin
garyaustin2mo ago
You still have not answered on if you changed the grants accidentally or on purpose. For instance you might have removed anon from that function so it could not be called by anon.
david_eason
david_easonOP2mo ago
I think I didn't delete it. Where I can add this again?
garyaustin
garyaustin2mo ago
So I'll give you some code. You really have not given me enough info (like running a select to see if it also fails) to know what grants were removed. This code in the SQL editor will restore public back to default grants.
GRANT USAGE ON SCHEMA public TO anon, authenticated, service_role;
GRANT ALL ON ALL TABLES IN SCHEMA public TO anon, authenticated, service_role;
GRANT ALL ON ALL ROUTINES IN SCHEMA public TO anon, authenticated, service_role;
GRANT ALL ON ALL SEQUENCES IN SCHEMA public TO anon, authenticated, service_role;
ALTER DEFAULT PRIVILEGES FOR ROLE postgres IN SCHEMA public GRANT ALL ON TABLES TO anon, authenticated, service_role;
ALTER DEFAULT PRIVILEGES FOR ROLE postgres IN SCHEMA public GRANT ALL ON ROUTINES TO anon, authenticated, service_role;
ALTER DEFAULT PRIVILEGES FOR ROLE postgres IN SCHEMA public GRANT ALL ON SEQUENCES TO anon, authenticated, service_role;
GRANT USAGE ON SCHEMA public TO anon, authenticated, service_role;
GRANT ALL ON ALL TABLES IN SCHEMA public TO anon, authenticated, service_role;
GRANT ALL ON ALL ROUTINES IN SCHEMA public TO anon, authenticated, service_role;
GRANT ALL ON ALL SEQUENCES IN SCHEMA public TO anon, authenticated, service_role;
ALTER DEFAULT PRIVILEGES FOR ROLE postgres IN SCHEMA public GRANT ALL ON TABLES TO anon, authenticated, service_role;
ALTER DEFAULT PRIVILEGES FOR ROLE postgres IN SCHEMA public GRANT ALL ON ROUTINES TO anon, authenticated, service_role;
ALTER DEFAULT PRIVILEGES FOR ROLE postgres IN SCHEMA public GRANT ALL ON SEQUENCES TO anon, authenticated, service_role;
david_eason
david_easonOP2mo ago
Thanks, let me try. Thank you very much. This is working well. After run this command, I can get successfully result. You are so cool Thanks I got so many stress from this problem and used many times. You solved only for a few mins without enough information. Thanks. If I have any problem again, can I ask you again? I am very beginner, so there are some problems to use Supabase.
garyaustin
garyaustin2mo ago
You should ask new questions each time. This is a user helping user forum and many users can help.
david_eason
david_easonOP2mo ago
Thanks. I like Supabase so much. It is very useful. Thanks again for your helping.
garyaustin
garyaustin2mo ago
Also you need to be careful on how your grants got removed as if your AI is telling you do that, it might be for some security reason.
david_eason
david_easonOP2mo ago
Yes
let { data: existingUserData, error: checkError } = await supabase
.rpc('get_user_by_email', {
user_email: email
})
if (checkError) console.error(checkError)
else console.log(existingUserData)
let { data: existingUserData, error: checkError } = await supabase
.rpc('get_user_by_email', {
user_email: email
})
if (checkError) console.error(checkError)
else console.log(existingUserData)
This code is wrong? in Postman, I got perfect result. But in my project, I got same issue again. 🙁 So sad
export const supabase = createClient(
DEFAULT_SUPABASE_URL,
DEFAULT_ANON_KEY
);
export const supabase = createClient(
DEFAULT_SUPABASE_URL,
DEFAULT_ANON_KEY
);
@garyaustin Please help me again. 🙏🏽 @garyaustin I am using Next.js
garyaustin
garyaustin2mo ago
I don't know use next.js or react. Did you check that DEFAULT_SUPABASE_URL is correct? Otherwise you will need to debug your code.
With Postman working it is not your project generating the issue and there are no bugs with supabase-js like this.
david_eason
david_easonOP2mo ago
Thanks. You are right. That's my fault. I put wrong supabase_url Thanks very much cool man. :supabase: Supabase is best

Did you find this page helpful?