Skip to content

Commit

Permalink
fix(query): log error about undefined query data being returned (#4218)
Browse files Browse the repository at this point in the history
* fix(query): log error about undefined query data being returned

* fix(query): fix test assertion
  • Loading branch information
TkDodo authored Sep 26, 2022
1 parent 4d68f00 commit 04945f3
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 10 deletions.
11 changes: 6 additions & 5 deletions packages/query-core/src/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -452,11 +452,12 @@ export class Query<
abort: abortController?.abort.bind(abortController),
onSuccess: (data) => {
if (typeof data === 'undefined') {
onError(
new Error(
`Query data cannot be undefined - affected query key: ${this.queryHash}`,
) as any,
)
if (process.env.NODE_ENV !== 'production') {
this.logger.error(
`Query data cannot be undefined. Please make sure to return a value other than undefined from your query function. Affected query key: ${this.queryHash}`,
)
}
onError(new Error('undefined') as any)
return
}

Expand Down
6 changes: 1 addition & 5 deletions packages/query-core/src/tests/query.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -816,11 +816,7 @@ describe('query', () => {

await sleep(10)

const error = new Error(
`Query data cannot be undefined - affected query key: ${
observer.getCurrentQuery().queryHash
}`,
)
const error = new Error('undefined')

expect(observerResult).toMatchObject({
isError: true,
Expand Down

0 comments on commit 04945f3

Please sign in to comment.