React Query v5.48.0 Cannot read properties of undefined (reading 'length') when using infinite queries #7633
Send to my agentDescribe the bug#
There is a resolved issue which is similar 6712, but the problem is that I am not using tanstack router. I am auto generating queries with kubb, and Im using Next. This issue is happening sometimes randomly.
Any suggestions or idea how to fix this?
import client from "../../client/axios-client";
import {
useQuery,
queryOptions,
useInfiniteQuery,
infiniteQueryOptions,
} from "@tanstack/react-query";
import type {
GetProjectsQueryResponse,
GetProjectsQueryParams,
GetProjects400,
} from "../types/GetProjects";
import type {
QueryObserverOptions,
UseQueryResult,
QueryKey,
InfiniteQueryObserverOptions,
UseInfiniteQueryResult,
InfiniteData,
} from "@tanstack/react-query";
type GetProjectsClient = typeof client<GetProjectsQueryResponse, GetProjects400, never>;
type GetProjects = {
data: GetProjectsQueryResponse;
error: GetProjects400;
request: never;
pathParams: never;
queryParams: GetProjectsQueryParams;
headerParams: never;
response: GetProjectsQueryResponse;
client: {
parameters: Partial<Parameters<GetProjectsClient>[0]>;
return: Awaited<ReturnType<GetProjectsClient>>;
};
};
export const getProjectsQueryKey = (params?: GetProjects["queryParams"]) =>
[{ url: "/projects" }, ...(params ? [params] : [])] as const;
export type GetProjectsQueryKey = ReturnType<typeof getProjectsQueryKey>;
export function getProjectsQueryOptions(
params?: GetProjects["queryParams"],
options: GetProjects["client"]["parameters"] = {}
) {
const queryKey = getProjectsQueryKey(params);
return queryOptions({
queryKey,
queryFn: async () => {
const res = await client<GetProjects["data"], GetProjects["error"]>({
method: "get",
url: `/projects`,
params,
...options,
});
return res.data;
},
});
}
/**
* @summary Get Project List
* @link /projects
*/
export function useGetProjects<
TData = GetProjects["response"],
TQueryData = GetProjects["response"],
TQueryKey extends QueryKey = GetProjectsQueryKey,
>(
params?: GetProjects["queryParams"],
options: {
query?: Partial<
QueryObserverOptions<
GetProjects["response"],
GetProjects["error"],
TData,
TQueryData,
TQueryKey
>
>;
client?: GetProjects["client"]["parameters"];
} = {}
): UseQueryResult<TData, GetProjects["error"]> & {
queryKey: TQueryKey;
} {
const { query: queryOptions, client: clientOptions = {} } = options ?? {};
const queryKey = queryOptions?.queryKey ?? getProjectsQueryKey(params);
const query = useQuery({
...(getProjectsQueryOptions(params, clientOptions) as unknown as QueryObserverOptions),
queryKey,
...(queryOptions as unknown as Omit<QueryObserverOptions, "queryKey">),
}) as UseQueryResult<TData, GetProjects["error"]> & {
queryKey: TQueryKey;
};
query.queryKey = queryKey as TQueryKey;
return query;
}
export const getProjectsInfiniteQueryKey = (params?: GetProjects["queryParams"]) =>
[{ url: "/projects" }, ...(params ? [params] : [])] as const;
export type GetProjectsInfiniteQueryKey = ReturnType<typeof getProjectsInfiniteQueryKey>;
export function getProjectsInfiniteQueryOptions(
params?: GetProjects["queryParams"],
options: GetProjects["client"]["parameters"] = {}
) {
const queryKey = getProjectsInfiniteQueryKey(params);
return infiniteQueryOptions({
queryKey,
queryFn: async ({ pageParam }) => {
const res = await client<GetProjects["data"], GetProjects["error"]>({
method: "get",
url: `/projects`,
...options,
params: {
...params,
["page"]: pageParam,
...(options.params || {}),
},
});
return res.data;
},
initialPageParam: 0,
getNextPageParam: (lastPage, _allPages, lastPageParam) =>
Array.isArray(lastPage) && lastPage.length === 0 ? undefined : lastPageParam + 1,
getPreviousPageParam: (_firstPage, _allPages, firstPageParam) =>
firstPageParam <= 1 ? undefined : firstPageParam - 1,
});
}
/**
* @summary Get Project List
* @link /projects
*/
export function useGetProjectsInfinite<
TData = InfiniteData<GetProjects["response"]>,
TQueryData = GetProjects["response"],
TQueryKey extends QueryKey = GetProjectsInfiniteQueryKey,
>(
params?: GetProjects["queryParams"],
options: {
query?: Partial<
InfiniteQueryObserverOptions<
GetProjects["response"],
GetProjects["error"],
TData,
TQueryData,
TQueryKey
>
>;
client?: GetProjects["client"]["parameters"];
} = {}
): UseInfiniteQueryResult<TData, GetProjects["error"]> & {
queryKey: TQueryKey;
} {
const { query: queryOptions, client: clientOptions = {} } = options ?? {};
const queryKey = queryOptions?.queryKey ?? getProjectsInfiniteQueryKey(params);
const query = useInfiniteQuery({
...(getProjectsInfiniteQueryOptions(
params,
clientOptions
) as unknown as InfiniteQueryObserverOptions),
queryKey,
...(queryOptions as unknown as Omit<InfiniteQueryObserverOptions, "queryKey">),
}) as UseInfiniteQueryResult<TData, GetProjects["error"]> & {
queryKey: TQueryKey;
};
query.queryKey = queryKey as TQueryKey;
return query;
}
`
### Your minimal, reproducible example
https://codesandbox.io/p/sandbox/
### Steps to reproduce
it is happening randomly
### Expected behavior
No error
### How often does this bug happen?
Sometimes
### Screenshots or Videos
<img width="765" alt="image" src="https://github.com/TanStack/query/assets/143590367/76edc309-9acb-4572-9ecb-cbbe7bf25d13">
<img width="699" alt="image" src="https://github.com/TanStack/query/assets/143590367/9dc4cf84-2130-4665-8d8e-0e3cacc5dac6">
### Platform
macOS
### Tanstack Query adapter
react-query
### TanStack Query version
v5.48.0
### TypeScript version
_No response_
### Additional context
_No response_TkDodo closed this
TkDodo added labelsneeds-info
@TkDodo yes that was the issue. Kubb generates the same key for infinite and "norma" queries. It was very hard to reproduce, I thought they have different keys.
Maybe someone who uses Kubb to auto-generate the queries might end up in the similar situation, changing the key did fix it.
Thanks 🤚
I guess you'd want to report this to
kubbThe most likely scenario is that you are using the same queryKey for a "normal" query and for the infinite query. They have different structures and you can't share the same key for both query types.
@TkDodo thanks for putting me on the right track with your comment! 🙏
It would've been very difficult to troubleshoot this from the error message and unhelpful stack trace alone.
The most likely scenario is that you are using the same queryKey for a "normal" query and for the infinite query. They have different structures and you can't share the same key for both query types.
Thank you for this Solution
Sign in to join this conversation.
The most likely scenario is that you are using the same queryKey for a "normal" query and for the infinite query. They have different structures and you can't share the same key for both query types.