1

I am sending a get request to API with axios , but unable to acess the passed parametrs in that request.

Here is my code: API request

 axios.get('http://localhost:4000/students',{
          params: {
            category:"indoor"
          }
        })

Request handling

router.route('/').get((req, res) => {
  console.log(req.params.category);
  studentSchema.find({category:req.params.category},(error, data) => {
    if (error) {
      return next(error)
    } else {
      res.json(data)
    }
  })
})

Routing is working properly when there is no parameters given.

console.log(req.params.category); is giving output as undefined.

Hope you got the Problem...

codemarvel
  • 57
  • 1
  • 8
  • What about the route? Shouldn't it be "/students" instead of "/"? – Wasbeer Nov 15 '20 at 08:20
  • @Wasbeer Routes are working fine.. I have already included ''/students" route in server.js file – codemarvel Nov 15 '20 at 08:32
  • Could you share your code? Either .zip or Github repo. – Wasbeer Nov 15 '20 at 08:49
  • Does this answer your question? [Node.js/Express routing with get params](https://stackoverflow.com/questions/8506658/node-js-express-routing-with-get-params) – xIsra Nov 15 '20 at 09:02
  • Please read about ExpressJS Params: http://expressjs.com/en/5x/api.html#req.params Body: http://expressjs.com/en/5x/api.html#req.body Mongoose Query: https://mongoosejs.com/docs/queries.html Axios Params: https://stackoverflow.com/questions/53501185/how-to-post-query-parameters-with-axios – xIsra Nov 15 '20 at 09:05

2 Answers2

1

I believe what you are trying to do has got nothing to do with Axios, To get query parameters, in the server side of your code, you need to use the property query of the request, like this:

let category = req.query["category"];
Edoardo
  • 4,485
  • 1
  • 27
  • 31
0

params in Axios are URL parameters that become a querystring.

params in Express' request object are route parameters. For example: When I define a URL such as /students/:id then req.params for GET /students/value will be {id: "value"}.

What you are looking for in Express handler is req.query [docs].

gurisko
  • 1,172
  • 8
  • 14