Accessing dynamic route using app route.
Answered
Asiatic Lion posted this in #help-forum
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
I keep getting
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 router6 Replies
@Asiatic Lion 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
ts
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.`
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 routerAnswer
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
But not this?
I'm not using the request itself, is it used internally?
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?
@Asiatic Lion Just a curious question.
Why does it work if i have it like this
ts
export async function GET(
request: Request,
{ params }: { params: { id: string } },
)
But not this?
ts
export async function GET({ params }: { params: { id: string } })
I'm not using the request itself, is it used internally?
because the first parameter is request and the second parameter is an object for dynamic segment
you can ignore the request if you are not using it but not omit
Original message was deleted
you should create your own thread