{"record":{"id":"1d7c3874e01a1706","repo":"TanStack/query","slug":"this-queryhash-data-is-undefined","errorCode":null,"errorMessage":"${this.queryHash} data is undefined","messagePattern":"(.+?) data is undefined","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"packages/query-core/src/query.ts","lineNumber":575,"sourceCode":"        this.#dispatch({ type: 'continue' })\n      },\n      retry: context.options.retry,\n      retryDelay: context.options.retryDelay,\n      networkMode: context.options.networkMode,\n      canRun: () => true,\n    })\n\n    try {\n      const data = await this.#retryer.start()\n      // this is more of a runtime guard\n      // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n      if (data === undefined) {\n        if (process.env.NODE_ENV !== 'production') {\n          console.error(\n            `Query data cannot be undefined. Please make sure to return a value other than undefined from your query function. Affected query key: ${this.queryHash}`,\n          )\n        }\n        throw new Error(`${this.queryHash} data is undefined`)\n      }\n\n      this.setData(data)\n\n      // Notify cache callback\n      this.#cache.config.onSuccess?.(data, this as Query<any, any, any, any>)\n      this.#cache.config.onSettled?.(\n        data,\n        this.state.error as any,\n        this as Query<any, any, any, any>,\n      )\n      return data\n    } catch (error) {\n      if (error instanceof CancelledError) {\n        if (error.silent) {\n          // silent cancellation implies a new fetch is going to be started,\n          // so we piggyback onto that promise\n          return this.#retryer.promise","sourceCodeStart":557,"sourceCodeEnd":593,"githubUrl":"https://github.com/TanStack/query/blob/159982c80b844487054155c38ffc760fe4208daf/packages/query-core/src/query.ts#L557-L593","documentation":"Runtime guard in `Query` (`@tanstack/query-core`) after `this.#retryer.start()` resolves. The query function MUST return a defined value; `undefined` is treated as 'no data' because it would make cache state ambiguous and break `data`/`status` semantics. The console error prints the affected `queryHash`, then the same message is thrown to fail the observer.","triggerScenarios":"A `queryFn` with an early `return;` or `return undefined`; a `queryFn` whose only `return` is inside a conditional branch that isn't taken; a `queryFn` that calls an API and forgets to return the response (`api.get()` without `return`); arrow-function shorthand that implicitly returns the wrong expression.","commonSituations":"Async functions where a code path falls through without returning; refactoring a queryFn and dropping the `return` keyword; `select` returning `undefined`; conditional fetch that returns nothing on a branch; integration tests with mock queryFns returning `undefined`.","solutions":["Inspect the queryFn for the matching `queryHash` and ensure every code path returns a non-undefined value.","Add `return data;` explicitly and avoid bare `return;`.","If absence is legitimate, model it as `null` and type `TData` accordingly, or split into a separate query that is conditionally enabled.","Add a test asserting the queryFn returns a defined value for the representative inputs."],"exampleFix":"// before\nqueryFn: async () => {\n  const res = await fetch('/api/user')\n  const json = await res.json()\n  // forgot to return\n}\n// after\nqueryFn: async () => {\n  const res = await fetch('/api/user')\n  return res.json()\n}","handlingStrategy":"validation","validationCode":"// Wrap your queryFn to assert a non-undefined return at runtime.\nfunction withDefinedReturn<T>(fn: () => Promise<T>): () => Promise<NonNullable<T>> {\n  return async () => {\n    const data = await fn()\n    if (data === undefined) throw new Error('queryFn returned undefined')\n    return data as NonNullable<T>\n  }\n}","typeGuard":null,"tryCatchPattern":"// In an observer error handler, surface the affected queryHash.\ncatch (e) {\n  if (e instanceof Error && /data is undefined/.test(e.message)) {\n    reportIssue(e.message) // contains the queryHash\n  }\n  throw e\n}","preventionTips":["Never write a queryFn with an implicit `undefined` return; always `return`.","Use `as const` or explicit return types so TypeScript flags undefined branches.","Add unit tests asserting the queryFn returns a defined value.","If absence is meaningful, model it with `null` and adjust `TData`."],"tags":["query-core","queryfn","data-contract","runtime-guard"],"backgroundTag":null,"analyzedSha":"159982c80b844487054155c38ffc760fe4208daf","analyzedAt":"2026-08-12T16:21:52.822Z","schemaVersion":2},"datasetVersion":"2026-08-12T18:17:37.767Z"}