Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

243
Views
Error object is null in useQuery's onError callback function (react-query)

I'm using react-query for data fetching. I wrote a useQuery function. I passed it an onError callback function that handle the errors that occur if the query fails.

Inside the onError function I access the property message of the err object. The problem is, sometimes I get an error Cannot read property message of null. Right before the access to the property, I printed the err object value and it is null. How can the error function get an error object that is null?

This is the code of the useQuery:

export function useGetSubject(subjectId: string) {
    const guid = useGuid();
    useEffect(() => {
        return () => store.dispatch(removeBackgroundIfErrorPresent(guid));
    }, [])
    return useQuery<Subject, Error>(['subject', subjectId], () => {
        if (subjectId) {
            return serverAccess.subject.viewSubject(subjectId)
        } else {
            return null
        }
    }, {
        refetchInterval: 60*1000,
        enabled: subjectId !== null,
        retry: (failureCount: number, error: Error) => {
            if (error && error.message === "Subject does not exist") {
                return false;
            }
            if (failureCount >= 3) {
                return false;
            }
            return true;
        }, 
        onSuccess: (data) => {
            store.dispatch(removeBackgroundErrorIfPresent(guid));
        },
        onError: (err) => {
            console.log("Error value", err) // When the bug occures, it prints null
            store.dispatch(addBackgroundError(err.message, guid))
        }
    });
}

Thank you !

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!