1

When I send an object as a parameter of my post or get request express doesn't seem to receive it

I have tried get and post requests both on the front end and on the server. All the dependencies are working fine (body-parser, etc.)

front end:

axios.get('http://localhost:4000/videoComments/comment', {pauseTime: 10})

or

axios.get('http://localhost:4000/videoComments/comment', {data:{pauseTime: 10}})

back-end:

videoCommentsRoutes.route('/comment').get(function (req, res) {
    console.log(req.body);

req.body is an empty object. req.data, req.params are all undefined

Chaim Ochs
  • 129
  • 2
  • 11
  • Try calling like this `axios.get('http://localhost:4000/videoComments/comment', {params:{pauseTime: 10}})`. You can access params at server by `req.params` – Revansiddh Apr 02 '19 at 13:35
  • 1
    See this answer https://stackoverflow.com/questions/6912584/how-to-get-get-query-string-variables-in-express-js-on-node-js – Luciano Semerini Apr 02 '19 at 13:39

2 Answers2

1

GET request supports only query parameters. axios (as well as any of fetch or XMLHTTPRequest wrappers, such as superagent) should transform your object into query string.

Try using req.query to get the query parameters. Here is express docs about it.

Limbo
  • 2,123
  • 21
  • 41
0

Back-End should be like

videoCommentsRoutes.route('/comment/:pauseTime').get(function (req, res) {
    console.log(req.params.pauseTime);
})

or

videoCommentsRoutes.route('/comment').get(function (req, res) {
        console.log(req.query.pauseTime);
    })

Front-end Call like

axios.get('http://localhost:4000/videoComments/comment', {params:{pauseTime: 10}})
Revansiddh
  • 2,932
  • 3
  • 19
  • 33