nextjs how do you get value from your query parameters?

 

Let's say you posted this /api/jobs/1234 in your nextjs endpoint. How do you get /1234 value? Since 1234 is auto map to id, you access it via id as shown in diagram below:

import { PrismaClient } from '@prisma/client'
import { NextApiRequest, NextApiResponse } from 'next'

export default async function handler(req: NextApiRequest, res: NextApiResponse) {
    console.log(req.query.id)
    res.json("hiring party id")
}

What if you have something like this api/jobs/1234?name=testdemoapp. Since it parse as a dictionary, you can access it using code below: 

import { PrismaClient } from '@prisma/client'
import { NextApiRequest, NextApiResponse } from 'next'

export default async function handler(req: NextApiRequest, res: NextApiResponse) {
    console.log(req.query.id)
    console.log(req.query.name)
    res.json("hiring party id")
}





Comments

Popular posts from this blog

The specified initialization vector (IV) does not match the block size for this algorithm