Next.js Discord

Discord Forum

fetching data

Unanswered
Exotic Shorthair posted this in #help-forum
Open in Discord
Exotic ShorthairOP
can I fetch in a next.js server component like this? to handle both local and prod instances?
    const response = await fetch('/api/companies')


i am getting this error though it does exist on the backend
Error fetching companies: [TypeError: Failed to parse URL from /api/companies] {
  [cause]: [TypeError: Invalid URL] {
    code: 'ERR_INVALID_URL',
    input: '/api/companies'
  }
}

5 Replies

Asian black bear
Having to use a fully qualified URL to your own server, assuming it's not a separate backend, is indicative of you doing it wrong. Basically don't fetch your own server in server-side code: https://nextjs-faq.com/fetch-api-in-rsc
@Asian black bear Having to use a fully qualified URL to your own server, assuming it's not a separate backend, is indicative of you doing it wrong. Basically don't fetch your own server in server-side code: https://nextjs-faq.com/fetch-api-in-rsc
Exotic ShorthairOP
i appreciate the feedback but I did not have access to the database in my project.

I had access to a production API hosted on retool.
I think my case falls in your assumption that, it is a seperate backend