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

471
Visualizações
Unhandled Rejection (TypeError): Cannot read properties of null (reading 'detail')

useEffect(() => { document.title = View Orders | Bothub;

fetch(`${backendAppUrl}/orders/all`, {
  ...getRequestParams("POST", {
    uid: localStorage.uid,
    idToken: localStorage.idToken,
    user: 0,
    pagination: 1,
  }),
})
  .then((res) => res.json())
  .then(
    (res) => {
      console.log(res);
      if (res.detail === "db-error" || res.detail === "forbidden") {
        setError(true);
        setLoading(false);
      } else {
        const val = res.data;
        setOrders(val);
        setLoading(false);
      }
    },
    (err) => {
      console.log(err);
      setError(true);
      setLoading(false);
    }
  );
// eslint-disable-next-line

}, []);

Why does it show like this and throw an error? Unhandled Rejection (TypeError): Cannot read properties of null (reading 'detail')

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

If this is showing null in the console:

console.log(res);

Then the JSON response was empty (though somehow still valid I guess) and there's no object to use. So the code needs to be able to handle a null value in res. For example:

if (res?.detail === "db-error" || res?.detail === "forbidden") {
  setError(true);
  setLoading(false);
} else {
  const val = res?.data;
  setOrders(val);
  setLoading(false);
}

This uses nullish-safe optional chaining to examine the properties of res or just return null. So when res is null then the if condition will be false and the else block will execute.

Alternatively, if you want the system to handle null differently, you'd perform that check in your logic. For example:

if (!res) {
  // respond to null in some way here
} else if (res.detail === "db-error" || res.detail === "forbidden") {
  setError(true);
  setLoading(false);
} else {
  const val = res.data;
  setOrders(val);
  setLoading(false);
}

Any way you structure it, the point is that res is null and your code assumes that it isn't, which produces the error.

about 4 years ago · Juan Pablo Isaza Relatório
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