Next.js Discord

Discord Forum

api route with custom params

Answered
Mugger Crocodile posted this in #help-forum
Open in Discord
Mugger CrocodileOP
I am having a route.ts file from app/api/feedback/[websiteId]/create file, how can I substract the websiteId from requests?

This is what I have:
export const POST = (req: NextApiRequest, res: Response, {params}: {params: {websiteId?: string}}) => {
    const {websiteId} = params;
    console.log(websiteId);
    return new Response(JSON.stringify({message: 'Feedback created'}))
}

but I keep getting this error
Cannot destructure property 'params' of 'undefined' as it is undefined
this is my api link: http://localhost:3000/api/feedback/salut/create
Answered by joulev
Remove the res: Response
View full answer

5 Replies

Mugger CrocodileOP
buuut, I also have this file in /api/script/[scriptId]
export const GET = async(req:Request, {params}: {params: {scriptId?: string}}) => {
  console.log(params.scriptId);
  const scriptCode = `
    function sayHello() {
      console.log("${params.scriptId}");
    }
  `;
  return new NextResponse(scriptCode, { headers: { 'Content-Type': 'application/javascript' } });
}
and it works as expectly
Answer
You got the type all wrong. In route handlers it’s not req, res, it’s just req. And the req is not a NextApiRequest, it’s just Request
Mugger CrocodileOP
uh ok
thanks