skip to Main Content

I am trying to update a post. this is the file structure: /api/posts/[postId]/route.ts. how can i get the postId in PATCH function in route.ts file?

// PATCH api/posts/3
export const PATCH = async (req: NextRequest, res: NextResponse) => {
    // get the `postId` somehow
} 

i tried req.nextUrl.searchParams(). but it only select query params.

2

Answers


  1. export const PATCH = async (
      request: Request,
      { params }: { params: { postId: string } }
    ) => {
      const postId = params.postId
    } 
    
    Login or Signup to reply.
  2. public static getOptionById = (
        req: Request
      ) => {
        const optionId = req.params['id']; 
        ...
      }
    

    in the router:

    OptionRouter.get("/getOptionById/:id", OptionsController.getOptionById);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search