Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

242
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda