paoloricciuti / sveltekit-search-params

The easiest way to read and WRITE from query parameters in sveltekit.
https://sveltekit-search-params.netlify.app
MIT License
497 stars 15 forks source link

Remove the field from the query parameters if it's null or undefined #1

Closed paoloricciuti closed 1 year ago

paoloricciuti commented 1 year ago

https://github.com/paoloricciuti/sveltekit-search-params/blob/2393a23fb54a187fdf7168823e58a4a26a696753/src/lib/sveltekit-search-params.ts#L105

brandonp-ais commented 1 year ago

Is this supposed to remove the parameter if the value of the parameter is null/undefined?

paoloricciuti commented 1 year ago

Is this supposed to remove the parameter if the value of the parameter is null/undefined?

It should but it also depends on other parts of the code...is this not happening?

paoramati commented 1 year ago

(personal account)

In my case it is not. I'll see if I can make a minimal repro.

paoramati commented 1 year ago

Here is a reproduction:

https://github.com/paoramati/sveltekit-search-params/tree/1_repro_undefined_not_deleting_query_params

Navigate to /params-issue

The error occurs when trying to update param values with null/undefined. Get this stacktrace (not sure if related yet).

Stack trace sveltekit-search-params.ts:129 Uncaught (in promise) TypeError: Cannot read properties of null (reading 'toString') at fnToCall (sveltekit-search-params.ts:129:45) at toBatch (sveltekit-search-params.ts:134:29) at sveltekit-search-params.ts:141:11 at Set.forEach () at sveltekit-search-params.ts:140:24

Some context: In the application I'm building, there is a search box + certain pages have their own search boxes. I want to remove params created by the opposing search if a user uses the other kind of search.

I will have a look if I can create a fix, but I'm not sure whether it's just my usage is off, or if there is an issue with queryParams not getting removed from URL when they should be.

Thanks!

brandonp-ais commented 1 year ago

I'm not sure what the intention of this line is tbh, but if I remove the ! and an undefined param/val is encountered, then it's removed how i would expect it to be removed.

https://github.com/paoloricciuti/sveltekit-search-params/blob/fcd9545f542bd9085625876313736f0a724533cc/src/lib/sveltekit-search-params.ts#L172

paoloricciuti commented 1 year ago

I'm not sure what the intention of this line is tbh, but if I remove the ! and an undefined param/val is encountered, then it's removed how i would expect it to be removed.

https://github.com/paoloricciuti/sveltekit-search-params/blob/fcd9545f542bd9085625876313736f0a724533cc/src/lib/sveltekit-search-params.ts#L172

Yup looks like a bug...it was previously !value[field] and I added == undefined and probably forgot to remove the !

Thanks for reporting and also find the solution. If you want to make a PR to be credited as a contributor feel free to do it otherwise I'll go ahead and fix it in a while

brandonp-ais commented 1 year ago

Cool, i'll make a PR tonight :)