Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

245
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda