Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

TanStack Query says the query function returned undefined

ErrorQuery data cannot be undefined. Please make sure to return a value other than undefined from your query function

Short 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.

  • Applies to TanStack Query 5, React 19, Next.js 15
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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)
  2. 2Match what you see, then jump to its fix

    1. 1The queryFn is an arrow function with { } or an async function that awaits but never returns.The function has braces but no returnFix
    2. 2The API or helper returns nothing for a missing record, or a field you read is absent from the response.An empty result is returned as undefinedFix

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/null

2Fix

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 dev

Confirmed by tanstack.com

Did this fix it?

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 dev

Confirmed by tanstack.com

Did this fix it?

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:

Related fixes

Web 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

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.