Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inconsistent behaviour of req.query #2661

Closed
kdzwinel opened this issue May 27, 2015 · 8 comments
Closed

Inconsistent behaviour of req.query #2661

kdzwinel opened this issue May 27, 2015 · 8 comments
Assignees

Comments

@kdzwinel
Copy link

Calling /?x[20]=20 returns req.query = { x: [ '20' ] }
Calling /?x[21]=21 returns req.query = { x: { '21': '21' } }

I just spent significant amount of time debugging that and it turns out that qs library is responsible:

qs will also limit specifying indices in an array to a maximum index of 20. Any array members with an index of greater than 20 will instead be converted to an object

This value (20) can be changed so that's not a problem. However, I believe this should be documented in express.

@dougwilson
Copy link
Contributor

Remember, you can always provide a function to app.set('query parser', fn) to parse the query however you like, including using qs with different settings besides the default:

app.set('query parser', function (str) {
  return qs.parse(str, {arrayLimit: 1000});
});

Otherwise, any documentation requests should be submitted to https://github.com/strongloop/expressjs.com :)

@kdzwinel
Copy link
Author

Thank you for a quick response! I've described this issue on stackoverflow so that it's googlable. This should be enough to help others that will run into this issue to get it fixed quickly.

@bartonsprings
Copy link

Hi,
I am trying to achieve the same but change the object depth. I put this piece of code before the routes but it does not work. I am sure some wrong setting on my part.

// Change default setting for query parser upto handle 10 deep objects   
app.set('query parser', function (str) {
    return qs.parse(str, { depth: 10 });
});

The depth still stayed at 5. And anything deeper gets setup as long string.

@dougwilson
Copy link
Contributor

@bartonsprings what exact version of Express are you using?

@bartonsprings
Copy link

"express": "^4.12.4",
"express-mysql-session": "^0.3.3",
"express-session": "^1.11.2",

@dougwilson
Copy link
Contributor

Also, @bartonsprings , that statement must be before the very first .use or any other route statement, or it won't work. If you provide your full, complete app, I would be able to tell you.

@bartonsprings
Copy link

Aah! Let me try that and report back the result.

@bartonsprings
Copy link

Great! works now. Thanks.
For others planning to do the same, ensure that you have var qs = require("qs"); included in the file.

@expressjs expressjs locked and limited conversation to collaborators Oct 9, 2015
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants