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

When an array has more than 21 items, it is parsed from a url string into an object, instead of array #324

Closed
carpben opened this issue Jul 24, 2019 · 8 comments

Comments

@carpben
Copy link

carpben commented Jul 24, 2019

Following an error that array.includes is not a function, it turns out that when an array which is longer than 21 items is translated into a url query string (using qs.stringify), and than the url string is parsed (using qs.parse) it returns an object, not an array. This can obviously cause issues when you expect an array and it's methods.

console.log( typeof(qs.parse(qs.stringify({activities:[....array 20 items long]})) ) // Array
console.log( typeof(qs.parse(qs.stringify({activities:[....array 22 items long]})) ) // Object
@ljharb
Copy link
Owner

ljharb commented Jul 24, 2019

This is the arrayLimit option, which is documented in the readme.

@samicodesit
Copy link

This is happening to me but with qs.stringify, not with parse
and I can see there's no arrayLimit option for the stringify method.

Any ideas?

@ljharb
Copy link
Owner

ljharb commented Jun 4, 2020

@samicodesit can you provide the exact code that you used with stringify? Filing it as a new issue would help.

@jmikrut
Copy link

jmikrut commented Feb 10, 2022

@ljharb — I can give some more info here.

@samicodesit is hitting the same issue that I am. Although arrayLimit is an option for qs.parse, it is not an option for qs.stringify. So, when we attempt to stringify an array with more than 20 items in the array, the function appears to use arrayFormat: 'indices' instead of the desired ``arrayFormat: 'brackets'`.

We just need an option to specify an arrayLimit in the stringify method.

Also, THANK YOU for this package. We are heavily using qs in Payload CMS and have just run into this same need.

UPDATE:

I just found that this was actually not caused by the qs.stringify method on our frontend app—instead, it was caused by the qs-middleware package that we used on the backend, which was responsible for parsing incoming query strings. I needed to set the arrayLimit in that middleware, in order to parse large arrays. I thought it was the frontend stringify but it was actually the backend.

@samicodesit you might be having the same issue?

@anuragsachdeva28
Copy link

Hey, I am facing the same issue, but in my case I am not using qs-middleware on my express server. I think there is something internal parsing happening from express. Can anyone help?

@alexmachina
Copy link

This should be reopened.
I am currently not able to stringify and parse an array with more than 21 items using qs, given that
qs.parse doesn't handle encoded comma when parsing array with comma option
#410

@ljharb
Copy link
Owner

ljharb commented Oct 26, 2023

@alexmachina if you set arrayLimit, it should handle it, and the issue you linked is fixed in v6.10.2+.

Please file a new issue if you're still having trouble with the latest version of qs.

@alexmachina
Copy link

Thanks! I solved it parsing with { arrayFormat: 'brackets'}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants