api route with custom params
Answered
Mugger Crocodile posted this in #help-forum
Mugger CrocodileOP
I am having a route.ts file from
This is what I have:
but I keep getting this error
this is my api link:
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 undefinedthis is my api link:
http://localhost:3000/api/feedback/salut/create5 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 expectlyYou 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