Next.js Discord

Discord Forum

Accessing dynamic route using app route.

Answered
Asiatic Lion posted this in #help-forum
Open in Discord
Asiatic LionOP
I've been at this for a couple of hours with no relief.

This is my project structure.

In my [id]-folder i have the route.ts, which contains the following minimal example of code

export async function GET(
    request: Request,
    res: NextApiResponse,
    { params }: { params: { id: string } },
) {
    const id = params.id; // 'a', 'b', or 'c'

    return res.status(200).json({ id });
}


I keep getting

TypeError: Cannot destructure property 'params' of 'undefined' as it is undefined.
Answered by Ray
it should be
export async function GET(
  request: Request,
  { params }: { params: { id: string } }
) {
  const slug = params.id // 'a', 'b', or 'c'
  return Response.json({ id }, { status: 200 })
}

NextApiResponse is for page router
View full answer

6 Replies

Answer
Asiatic LionOP
Yea that worked a lot better, thanks as always ray
Asiatic LionOP
Just a curious question.

Why does it work if i have it like this

export async function GET(
    request: Request,
    { params }: { params: { id: string } },
)


But not this?

export async function GET({ params }: { params: { id: string } }) 


I'm not using the request itself, is it used internally?
Original message was deleted
you should create your own thread