#why do i not see postId
1 messages · Page 1 of 1 (latest)
Based on the information provided in the extracts, it seems like you're trying to access route parameters in a Next.js API route. The way you're trying to destructure postId from params should work if params contains a postId property.
However, if postId is undefined, it could mean that the postId parameter is not being correctly passed in the route. In Next.js, dynamic route parameters are based on the file and folder names in the pages directory. For example, a file named pages/api/post/[postId].ts would match the route /api/post/abc and postId would be "abc".
If you're not seeing the expected postId, make sure that:
- Your file is correctly named with the dynamic segment (e.g.,
[postId].ts). - You're making a request to the correct route that includes the
postId(e.g.,/api/post/abc).
If you're still having issues, it might be helpful to check the value of params to see what data it contains. If params is an empty object or does not contain a postId property, there might be an issue with how your routes are set up.
For more information on dynamic routes in Next.js, you can refer to the Dynamic Routing documentation (https://nextjs.org/docs/pages/building-your-application/routing/dynamic-routes).
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@gaunt frost kapa.ai is still learning and improving, please let us know how it did by reacting below
@safe spade my route looks like /api/posts/[postId]/like
Let me check the knowledge base for you. One moment! :books:
If your route is /api/posts/[postId]/like, then your file structure should look like this: pages/api/posts/[postId]/like.ts.
In this case, you should be able to access postId from the params object in your API route handler. Here's an example of how you might structure your handler:
export default function handler(req: NextApiRequest, res: NextApiResponse) {
const { postId } = req.query;
console.log(postId);
// ...rest of your code
}
In this example, postId is extracted from req.query, which should contain all the dynamic route parameters.
If postId is still undefined, make sure you're making a request to the correct route (e.g., /api/posts/abc/like), and that your file is correctly placed in the pages/api/posts/[postId] directory with the name like.ts.