-
-
Notifications
You must be signed in to change notification settings - Fork 16.7k
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
In Express 5, how to make a query param parsed as an array when it is an array of string of one element ? #6207
Comments
First, just to clear up any confusion, it does not parse the url every time: https://github.com/pillarjs/parseurl/blob/master/index.js#L43 Second, seems like this lib is breaking some assumptions because of this misunderstanding, but I will not comment more on that other than to say this seems like a bad idea on their part. But this doesn't appear to have anything to do with the question you asked, which is about the breaking changes from The query string parser would not treat this as an array because it has no indication of it being an array, so the best solution if you always want that to be an array is to check and make it an array if it is not:
|
Migrating from Express 4 to Express 5 introduced some changes to how query parameters are parsed, particularly with regards to arrays and single-element arrays. This behavior stems from the underlying Why the Issue OccursExpress 4 uses SolutionsHere are clean and practical solutions for resolving this issue: 1. Use the
|
Hi @kerlos-alfy, Thank you for your solutions ! However they don't work... Before asking my question I had tried the first one already, and the second one does not work. I didn't try the last one since I don't want that all the query params to be treaded as array. I think it is because the problem is not the query parser, the problem is about the parameter "query" of the object "request" - which is actually not an object param in Express 5, but a getter. And this getter does not only "get" the param, it parses the query from the url and makes some transformation - and I think this is why I have the problem. How the query param is returned in Express 4
How the query param is returned in Express 5
Thank you for your answer ! |
Hello @wesleytodd, Thank you for your answer ! I don't want my query params to be always treated as array... So maybe there is no solution to my problem... Thank you ! |
@kerlos-alfy does that mean, that qs is always installed even tho its not used by default? I still see it listed in the package.json and it comes with so much baggage: https://npmgraph.js.org/?q=qs // EDIT: realized that the answer looks like chatgpt... so nvm |
Hello everybody,
I am currently migrating my app from Express 4.17.21 to Express 5. I am using Node 22.9. The only problem I have is for parsing the query params when it is array of string of 1 element.
Here is an example of the url :
The query param firstElement is not recognized as a an array, and it was the case in Express 4.x.x.
This problem can be fixed by this code (found https://github.com/cdimascio/express-openapi-validator/issues/599 thanks to @djMax)
But this is not very clean.
I know if I use the "extended" query parser (app.set("query parser", "extended")), and use the url
the problem can be fixed but for reasons - such as our APIs are used by consumers - I cannot use this solution.
So I wonder whether there is a "clean" way for parsing as Express 4 does...?
Thank you !
The text was updated successfully, but these errors were encountered: