Next.js Discord

Discord Forum

Why params in page server route need to awaited?

Unanswered
Saltwater Crocodile posted this in #help-forum
Open in Discord
Saltwater CrocodileOP
I have a single page handle a fetching a data with a parameter, but it return error for the params id
Error: Route "/member/[memberid]" used should be awaited before using its properties. Learn more: https://nextjs.org/docs/messages/sync-dynamic-apis
at Page (page.tsx:7:28)
at JSON.parse (<anonymous>)

16 Replies

Saltwater CrocodileOP
Do you have docs for itm
Last time is i use 13 next version
Roseate Spoonbill
@Saltwater Crocodile TL; DR - async calls are used by Next.js to determine if your route has dynamic content. Previously they had to keep a list of functions that will make the page dynamic. Now it is done simply by checking if some things are awaited, which makes it easier for Next.js devs to cover all possible dynamic content scenarios.

Since params dictate the result of rendered page, it got promisified to match the above rule.
@Yi Lon Ma https://nextjs.org/docs/app/building-your-application/routing/dynamic-routes
Saltwater CrocodileOP
thanks my brother you keep helping me this week
Saltwater CrocodileOP
guys why i have error when build
app/(member)/member/[memberid]/[name]/@funfact/page.tsx
Type error: Type '{ params: { memberid: string; name: string; }; }' does not satisfy the constraint 'PageProps'.
Types of property 'params' are incompatible.
Type '{ memberid: string; name: string; }' is missing the following properties from type 'Promise<any>': then, catch, finally, [Symbol.toStringTag]
with this
const id = (await params).memberid;
const galleryResponse = await GetGallery(id);

the problem if we dont do await for the id it will crash like before
your types
Saltwater CrocodileOP
isnt already string ?
Short mackerel
You have two choices how to solve it.
Either you can solve it like this:
type LandingPageProps = {
  params: Promise<{
    slug: string;
    locale: string;
  }>;
};

export default async function LandingPage({ params }: LandingPageProps) {
  const { slug, locale } = await params;

...
}


Or you can set a custom middleware header in where you can access it in the server component directly.
Roseate Spoonbill
@Saltwater Crocodile For clarity, this is what happens in the code above and what you need to do in your code:
Replace { params: { memberid: string; name: string; }; } with { params: Promise<{ memberid: string; name: string; }>; }

Sorry if you already understood the message above, but I think it's worth mentioning what part of the code above is actual solution. The example provided is with a good practice, which assignes the types of parameters to a named alias - this clears the component definition a lot and I suggest doing the same.
Saltwater CrocodileOP
guys thank you so much
i just found i have second request forgot to give promise
really really thank you