TanStack Query says the query function returned undefined
Query data cannot be undefined. Please make sure to return a value other than undefined from your query functionShort answer
TanStack Query treats a query that resolves to undefined as failed. Make the query function return the data: add the missing return in a braced arrow function, and when a result is legitimately empty, return null instead of undefined. Also throw on bad HTTP status, because fetch does not.
Fix it fast
1Find out which cause you have
Call the query function by hand and look at what it resolves to.
// in a scratch script or the browser console const result = await fetchTodos() console.log(result)2Match what you see, then jump to its fix
What does the error look like?
Query data cannot be undefined. Please make sure to return a value other than undefined from your query function
Cause 1 of 2
The function has braces but no return
1Check
The promise should either resolve the data or throw an error. A function with no return resolves to undefined.
grep -rn -A4 "queryFn" app components src 2>/dev/null2Fix
Return the value.
// wrong
queryFn: async () => { await api.getTodos() }
// right
queryFn: async () => {
const todos = await api.getTodos()
return todos
}3Verify
Reload the page; the query leaves the error state and data is filled.
npm run devConfirmed by tanstack.com
Cause 2 of 2
An empty result is returned as undefined
1Check
Queries that resolve to undefined are treated as failed, so an empty answer must be an explicit value.
console.log(JSON.stringify(await fetchUser(id)))2Fix
Return null for no result, or a default such as an empty array. Throw on a failed HTTP status as fetch does not.
queryFn: async () => {
const response = await fetch(`/api/users/${id}`)
if (!response.ok) {
throw new Error('Network response was not ok')
}
const user = await response.json()
return user ?? null
}3Verify
A request for a missing user now succeeds with null instead of failing.
npm run devConfirmed by tanstack.com
How do I stop it coming back?
- Type the query function's return so TypeScript flags a missing return.
- Use null for no value, never undefined.
- Check response.ok before parsing and throw so errors reach the query's error state.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- TaskvareA task and project-management suite: React on the web, Flutter on mobile, TypeScript underneath.
Related fixes
Web apps
onSuccess and onError on useQuery do nothing after upgrading TanStack Query
onError callback not working after upgrading to React Query v5
TanStack Query v5 removed onSuccess, onError and onSettled from queries, so options passed to useQuery are ignored.
2 causes, each with a fixWeb apps
ESLint says an element in a list is missing its key prop
react/jsx-key
react/jsx-key fires when an element returned from map, Array.from or an array literal has no key.
3 causes, each with a fixWeb apps
There is no tailwind.config.js after installing Tailwind 4 and HeroUI in Next.js
HeroUI components render unstyled
Nothing is broken: Tailwind CSS 4 is configured in CSS and no longer creates tailwind.config.js.
3 causes, each with a fixWeb apps
react-i18next shows the wrong plural form for a count, or prints the key instead of the text
t('item') prints the key item instead of a translated sentence
i18next picks a plural from the count option and looks for keys ending in _one, _other and so on.
4 causes, each with a fixWeb apps
useSession throws because there is no SessionProvider above it
[next-auth]: `useSession` must be wrapped in a <SessionProvider />
useSession reads the session from React Context, and SessionProvider is what creates that context.
3 causes, each with a fixWeb apps
next/link throws React.Children.only on its children
Error: React.Children.only expected to receive a single React element child.
React.Children.only accepts exactly one React element and throws for anything else, including an array with one item.
3 causes, each with a fix