How should I use createRouteData

export function routeData({ params, location }: RouteDataArgs) {
return createServerData$(
async ([slug, page]) => {
const response = await fetch(`/api/list`, {
method: "POST",
body: JSON.stringify({
keyword: slug,
page: page
})
});
return (await response.json());
},
{ key: () => [params.slug, location.query['page'] || "1"] }
);
}
export function routeData({ params, location }: RouteDataArgs) {
return createServerData$(
async ([slug, page]) => {
const response = await fetch(`/api/list`, {
method: "POST",
body: JSON.stringify({
keyword: slug,
page: page
})
});
return (await response.json());
},
{ key: () => [params.slug, location.query['page'] || "1"] }
);
}
How to request the internal API must write the full URL such as http://localhost:3000/api/list
5 Replies
NesCafe
NesCafe17mo ago
I dont know much, but maybe because this function is async async ([slug, page]) => { it expects return value to be a Promise. but currently there is return (await response.json()); maybe try to just return json?
return response.json();
return response.json();
actually... is that response type not a Promise also? because you made await there... const response = await fetch
haikesikejiqiang
haikesikejiqiangOP17mo ago
i don't think so。I write the request address as http://localhost:3000/api/list and no error will be reported. I want to know if I have to write all ryan
NesCafe
NesCafe17mo ago
ah ok so this code is from an example btw you can check network tab in browser devtools from my experience if you call a fetch with some absolute url but without specifying protocol and domain it should just grab it from the current address the only thing that can change it is use of base tag in page header
haikesikejiqiang
haikesikejiqiangOP17mo ago
GitHub
[Bug?]: Why must an absolute path be used to create routing data · ...
Duplicates I have searched the existing issues Latest version I have tested the latest version Current behavior 😯 export function routeData({ params, location }: RouteDataArgs) { return createServe...
NesCafe
NesCafe17mo ago
wow
Want results from more Discord servers?
Add your server