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

11 Replies
stormy-goldOP•10mo ago
Opening the link in a new tab works though XD
stormy-goldOP•10mo 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?

cloudy-cyan•10mo ago
can you please provide a minimal complete example?
search middlewares cannot return
undefined
, so this should not happenstormy-goldOP•10mo 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
cloudy-cyan•10mo ago
still, this should not occur
so a reproducer would be very helpful
stormy-goldOP•10mo 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
cloudy-cyan•10mo ago
"make all params optional"
your code does not do this
can you please fork an example and apply all the necessary changes?
stormy-goldOP•10mo ago
Go to users and hit the refresh button
cloudy-cyan•10mo ago
you did not supply the search params
so you get a zod error
when search validation fails, middlewares will not execute properly
stormy-goldOP•10mo 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