codemarvel
codemarvel

Reputation: 57

How to acess axios params in nodejs get request

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...

Upvotes: 1

Views: 6479

Answers (2)

Edoardo
Edoardo

Reputation: 5442

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"];

Upvotes: 1

gurisko
gurisko

Reputation: 1182

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].

Upvotes: 0

Related Questions