failed to parse URL
Answered
Elite posted this in #help-forum
EliteOP
its a page that does stuff based off of url (slug)
so if i go to example.com/partner/123
it queries 123 in my db
I tried adding
error: An error occurred while fetching from the DB: TypeError: Failed to parse URL from /api/db/fetch-all-rows-with-user
code: https://gist.github.com/navincodesalot/03bbdd30fd95c81e1e4bbd1f565a3bdd
so if i go to example.com/partner/123
it queries 123 in my db
I tried adding
export const dynamic = 'force-dynamic'; to the top of my page.tsx but it doesn't do anything.error: An error occurred while fetching from the DB: TypeError: Failed to parse URL from /api/db/fetch-all-rows-with-user
code: https://gist.github.com/navincodesalot/03bbdd30fd95c81e1e4bbd1f565a3bdd
Answered by American Crow
Don't fetch your own endpoint in a server component. Either use the logic of
fetch-all-rows-with-user directly within your server component or use a server action. You can read more about it in detail here: https://nextjs-faq.com/fetch-api-in-rsc25 Replies
American Crow
Don't fetch your own endpoint in a server component. Either use the logic of
fetch-all-rows-with-user directly within your server component or use a server action. You can read more about it in detail here: https://nextjs-faq.com/fetch-api-in-rscAnswer
@American Crow Don't fetch your own endpoint in a server component. Either use the logic of `fetch-all-rows-with-user` directly within your server component or use a server action. You can read more about it in detail here: https://nextjs-faq.com/fetch-api-in-rsc
EliteOP
so should i make all of my page.tsx server components?
and then my components inside of that are client if needed?
and then my components inside of that are client if needed?
cause rn all my pages are server, even if the interior componenets are clients
so then should i use a server action to fetch data?
American Crow
yes, most of your page.tsx will be server components. Fetch data or make database call directly in the server component. pass data down to the client component(s)
EliteOP
alr so dont do like a fetch request
is it like better formatting if i make them server actions?
cause i like the way how my api routes are organized rn
but obv i cant fetch in server components
so should i make server actions?
but obv i cant fetch in server components
so should i make server actions?
American Crow
As a beginner rule: If its GET getting data, do it in a server component. If its manipulating data (post/put/create) go for a server action
EliteOP
alr
is there an example of a server action psot request on the link u sent?
American Crow
No it exaplains getting data so its not a serer action
here are plenty of examples for server actions: https://nextjs.org/docs/app/building-your-application/data-fetching/server-actions-and-mutations
EliteOP
alr ty
was really confused why this didnt work lol
was beign stupid
was beign stupid
@American Crow As a beginner rule: If its GET getting data, do it in a server component. If its manipulating data (post/put/create) go for a server action
EliteOP
wait so lets say i wanted to fetch like the openai api
im able to do that with a fetch in a server component
just not my own route handlers right?
im able to do that with a fetch in a server component
just not my own route handlers right?
American Crow
yes you are correct
@American Crow yes you are correct
EliteOP
also rn im like setting a variable called data
i cant do that with server actions right?
how should i go about doing that?
https://gist.github.com/navincodesalot/ebc6a969514d4c0e5297b65d02036b44
sorry for the extra question
i cant do that with server actions right?
how should i go about doing that?
https://gist.github.com/navincodesalot/ebc6a969514d4c0e5297b65d02036b44
sorry for the extra question
@American Crow yes you are correct
EliteOP
hey so do i not fetch route handlers in these actions on the server side?
@Elite hey so do i not fetch route handlers in these actions on the server side?
American Crow
no you don't. I dont know how to explain it differently. I am gonna link you a video with an overview of when to use what https://www.youtube.com/watch?v=4pmPaAFB6ik
@Elite hey so do i not fetch route handlers in these actions on the server side?
American Crow
I assume you are trying to GET data since your function is called
I dont know what Database or ORM you are using but here is some pseudo code:
Thats it no server action no endpoint
fetchAllTableDAta and your variable getAllRows.I dont know what Database or ORM you are using but here is some pseudo code:
export default async function ExplorePage() {
const {userId} = auth()
const allRows = await db.findMany("myTable", userId)
return (
<ExploreTable data={allRows} />
)
}Thats it no server action no endpoint
EliteOP
its a get request but the route handler is a POST as it needs a body of data.
but i see
if its a server component, and its internal, i just do the logic above it without server actions or whatnot
but i see
if its a server component, and its internal, i just do the logic above it without server actions or whatnot
@American Crow I assume you are trying to GET data since your function is called `fetchAllTableDAta` and your variable `getAllRows`.
I dont know what Database or ORM you are using but here is some pseudo code:
tsx
export default async function ExplorePage() {
const {userId} = auth()
const allRows = await db.findMany("myTable", userId)
return (
<ExploreTable data={allRows} />
)
}
Thats it no server action no endpoint
EliteOP
my question is:
if all my page.tsx are server components,
then theres no point to having route handlers then right?
cause all the logic can happen in the
unless I have a client page.tsx
then I can do a fetch request from the client side if I want. or a server action running on the server
right?
if all my page.tsx are server components,
then theres no point to having route handlers then right?
cause all the logic can happen in the
lib or util folders right?unless I have a client page.tsx
then I can do a fetch request from the client side if I want. or a server action running on the server
right?
@Elite my question is:
if all my page.tsx are server components,
then theres no point to having route handlers then right?
cause all the logic can happen in the `lib` or `util` folders right?
unless I have a client page.tsx
then I can do a fetch request from the client side if I want. or a server action running on the server
right?
American Crow
If your page.tsx is a server component. You do all the logic directly inside it not even in
If you have a client component it depends. If the client component needs data. Its a server components job to get that data and pass it down to it.
If that client component has to mutate data (update,delete,create) a server action is the way to go.
If you want to fetch from an external API within your client component you can use libaries like useQuery or useSwr (the way you did it in the past)
libor util just in it's body.If you have a client component it depends. If the client component needs data. Its a server components job to get that data and pass it down to it.
If that client component has to mutate data (update,delete,create) a server action is the way to go.
If you want to fetch from an external API within your client component you can use libaries like useQuery or useSwr (the way you did it in the past)
@American Crow If your page.tsx is a server component. You do all the logic directly inside it not even in `lib`or `util` just in it's body.
If you have a client component it depends. If the client component needs data. Its a server components job to get that data and pass it down to it.
If that client component has to mutate data (update,delete,create) a server action is the way to go.
If you want to fetch from an external API within your client component you can use libaries like useQuery or useSwr (the way you did it in the past)
EliteOP
Ok so I want to in the future make my server side logic public to users. Like an api for them to use.
How would I build that with nextjs natively?
Like if I don’t have any route handlers. And all my logic is above the return method of a page.tsx(because all of my pages are server components with child components that may be client)
How would I build that with nextjs natively?
Like if I don’t have any route handlers. And all my logic is above the return method of a page.tsx(because all of my pages are server components with child components that may be client)