Home > front end >  Optional Parameter is undefined
Optional Parameter is undefined

Time:06-13

Im trying to set up a route that has an optional parameter status. This is the route:

app
    .route('/trips/:status?')
    .get(getManyTripsCommand)

this is the function:

async function getManyTripsCommand(request, response) {
    console.log(request.params)
    const status = request.params.status

    try {
        const trips = await getManyTrips(status)
        return response.status(201).json(trips)
    } catch (err) {
        return response.status(err.code).json({ status: err.code, message: err.message })
    }

}

and this is the request:

localhost:3002/trips?status="active"

in getManyTripsCommand, request.params.status is undefined. any ideas?

CodePudding user response:

Use query instead of params

request.query.status

Here are more details: https://stackabuse.com/get-query-strings-and-parameters-in-express-js/

  • Related