T
TanStack13mo ago
fascinating-indigo

Retain search params: Cannot use 'in' operator to search for 'rf' in undefined

I get the following error when trying to navigate to a page by using retainSearchParams. Is this a bug or am I doing something wrong? I have the desired parameter defined in a zod schema and in the retainSearchParams array
No description
11 Replies
fascinating-indigo
fascinating-indigoOP13mo ago
Opening the link in a new tab works though XD
fascinating-indigo
fascinating-indigoOP13mo ago
The problem is in this piece of code If I don't have any search params on the link it returns undefined. @Manuel Schiller I see you are the author of the search middleware. What do you think about this?
No description
xenophobic-harlequin
xenophobic-harlequin13mo ago
can you please provide a minimal complete example? search middlewares cannot return undefined, so this should not happen
fascinating-indigo
fascinating-indigoOP13mo ago
I found out that if the search params are optional this does not happen. If they are mandatory this is reproducible. I'll try to provide an example in the following days. But anyway I can write my custom middleware which satisfies my requirements
xenophobic-harlequin
xenophobic-harlequin13mo ago
still, this should not occur so a reproducer would be very helpful
fascinating-indigo
fascinating-indigoOP13mo ago
Use this example https://tanstack.com/router/latest/docs/framework/react/examples/kitchen-sink-file-based Go to dashboard.users.tsx and do this (make all params optional) validateSearch: z.object({ usersView: z .object({ sortBy: z.enum(['name', 'id', 'email']), filterBy: z.string(), }), }).parse, Navigate to dashboard users and observer the error @Manuel Schiller
xenophobic-harlequin
xenophobic-harlequin13mo ago
"make all params optional" your code does not do this can you please fork an example and apply all the necessary changes?
fascinating-indigo
fascinating-indigoOP13mo ago
Go to users and hit the refresh button
xenophobic-harlequin
xenophobic-harlequin13mo ago
you did not supply the search params so you get a zod error when search validation fails, middlewares will not execute properly
fascinating-indigo
fascinating-indigoOP13mo ago
Okay so I've added a Test link on the sidebar. It will provide the search params the dashboard.users.tsx expects. The idea is to retain them but click on the Test link, go to users and see the error https://stackblitz.com/edit/tanstack-router-du14qg?file=src%2Froutes%2F__root.tsx,src%2Froutes%2Fdashboard.users.tsx&preset=node

Did you find this page helpful?