georgesamper
georgesamper

Reputation: 5179

Express multiple query parameters with same name does not return array

With this url http://example.com/users?test=1&test=2

router.route('/users/?').get((req, res) => {
  console.dir(req.query) //=> { test : 1 }
})

returns { test : 1 } instead of an array with [ 1, 2 ] as it should.

?test[]=1&test[]=2 doesn't work either.

What could be wrong here?

Upvotes: 1

Views: 2668

Answers (1)

jfriend00
jfriend00

Reputation: 708046

When I do this: http://localhost/?test[]=1&test[]=2, it works just fine for me. req.query contains:

{ test: [ '1', '2' ] }. 

In fact, http://localhost/?test=1&test=2 also generates the same

{ test: [ '1', '2' ] } 

result for me. I'm running Express 4.17.1. So, if you have the proper Express configuration, it will work.

Notice also, that I'm getting the results as strings and you are getting the results as numbers so you are apparently not using the default Express parsing for query parameters.

Something in your express configuration must have a different setting for how query parameters are parsed.

Upvotes: 3

Related Questions