I have the below function in app/api/hello/[slug]/route.ts
when I console log I get undefined . why ???
app/api/hello/[slug]/route.ts
export async function GET({ params }: any) {
console.log(params);
}
log
- wait compiling /api/hello/[slug]/route (client and server)...
- event compiled successfully in 308 ms (65 modules)
undefined
2
Answers
Because you are destructuring the Request object (which has no params key)
The parameters object is the second argument.
You may have a look at the NextJS documentation about Dynamic Route Segments:
https://nextjs.org/docs/app/building-your-application/routing/router-handlers#dynamic-route-segments
It is because first param is
Request
And params is 2nd param (optional)
So the final code should be:
You can make it specific by specifying the type of slug