query icon indicating copy to clipboard operation
query copied to clipboard

pages in the getNextPageParam method in infiniteQueryBehavior is undefined.

Open arianshi opened this issue 2 years ago • 4 comments

Describe the bug

We call the useInfiniteQuery function in @tanstack/react-query": "5.13.4":

const { refetch, data, isLoading, isFetching, isFetchingNextPage, fetchNextPage, hasNextPage, isSuccess, isFetched, error, isError, } = useInfiniteQuery({ queryKey: cacheKey, queryFn: async ({ pageParam }) => { const params = { [nextPageParam]: pageParam || nextPageDefault, limit: 50, reactions: isSubscription ? {} : { counts: true, own: true }, ...(ranking && { ranking }), }

  let data = result?.results || []
  data = data.filter(
    (activity) => activity.object && typeof activity.object === 'object'
  )
  if (appConfig.disableRecommendations) {
    data = removeRecommendations(data)
  }
  cacheActivitiesReactions(data)
  return {
    data,
    next: result?.next ? parse(result?.next)?.[nextPageParam] : undefined,
  }
},
enabled: !isOnline || (!!client && enabled),
getNextPageParam: (lastPage) => lastPage?.next,
initialPageParam: '',

})

Will appear:

image

We tried using useInfiniteQuery to cache data and do infinite fetching of data but occasionally it doesn't work

Page in getNextPageParam method in infiniteQueryBehavior is undefined

https://github.com/TanStack/query/blob/main/packages/query-core/src/infiniteQueryBehavior.ts#L150

The robustness of this line of code is very poor.

This function directly passes a data to getNextPageParam and then deconstructs it in getNextPageParam. How to ensure that pages is always an array and not undefined?

function hasNextPage(options, data) {
  if (!data)
    return false;
  return getNextPageParam(options, data) != null;
}

function getNextPageParam(options, { pages, pageParams }) {
  const lastIndex = pages.length - 1;
  return options.getNextPageParam(
    pages[lastIndex],
    pages,
    pageParams[lastIndex],
    pageParams
  );
}

image

Your minimal, reproducible example

This problem is difficult to reproduce and currently we cannot provide a small demo.

Steps to reproduce

infiniteQueryBehavior.ts:140 Uncaught TypeError: Cannot read properties of undefined (reading 'length') at getNextPageParam (infiniteQueryBehavior.ts:140:27) at hasNextPage (infiniteQueryBehavior.ts:169:10) at InfiniteQueryObserver.createResult (infiniteQueryObserver.ts:163:20) at InfiniteQueryObserver.updateResult (queryObserver.ts:591:29) at InfiniteQueryObserver.setOptions (queryObserver.ts:196:10) at InfiniteQueryObserver.setOptions (infiniteQueryObserver.ts:84:11) at new _a9 (queryObserver.ts:93:10) at new InfiniteQueryObserver (infiniteQueryObserver.ts:64:5) at useBaseQuery.ts:59:7 at mountState (react-dom.development.js:16986:20)

image

Expected behavior

Ensure pages are not undefined

How often does this bug happen?

Occasional

Screenshots or Videos

image

Platform

Chrome Version 124.0.6367.62 (Official Build) (arm64)

Tanstack Query adapter

None

TanStack Query version

@tanstack/react-query": "5.13.4"

TypeScript version

"typescript": "5.2.2",

Additional context

NO

arianshi avatar May 03 '24 02:05 arianshi

we only call the getNextPageParam function after we've already successfully fetched at least one page, so the access you're showing is safe. Also, we have tests for this.

the place your stacktrace points to comes likely from here:

https://github.com/TanStack/query/blob/39b2f81bc782a8633bd601b6d2e35d2a8c2ec4c0/packages/query-core/src/infiniteQueryBehavior.ts#L174-L180

so I think you wind up with corrupted data in your cache, e.g. an empty array that doesn't conform to the InfiniteData type structure. This should be trivial to debug by looking into the query devtools or by setting a logpoint / breakpoint at the location where the error occurs.

one wild guess is that you call setQueryData on this entry, or have a global intitialData that gets applied and puts the wrong structure in the cache. This is as far as my guesswork goes without seeing a proper reproduction. If you file another issue about this without showing a reproduction, I will close it without comment.

TkDodo avatar May 03 '24 18:05 TkDodo

I have this problem today. It occured because the queryKey is the same as another query ( not an infinite one)

meotimdihia avatar May 04 '24 15:05 meotimdihia

keys need to be unique, see: https://tkdodo.eu/blog/effective-react-query-keys#caching-data

TkDodo avatar May 05 '24 10:05 TkDodo

I'm having this error in a React Native app. I've already checked and confirmed that I'm not sharing a queryKey between a query and an infinite query. This does not always happen, it is an intermitent error, hard to debug.

Something similar is dicussed here #8825.

Is there any workaround until a fix is implemented?

reggiegutter avatar Jun 30 '25 18:06 reggiegutter