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

278
Visualizações
Axios does not send appropriate error response [React]

I'm trying to handle the error responses from Axios with React, but when i try to console log it the error is Network error and the error.response is undefined.

The request is a simple POST with the Authorization header correctly setted, when I try to do the same request on Postman it works correctly and I'am able to see the error response.

The request is made after the user fills a form and click on a button.

async function create() {
  const response = await Axios.post(
    "/api/disposal-requests/", // the base url is setted when the application mounts `Axios.defaults.baseURL = process.env.REACT_APP_API_URL`
    {
      description: "Description",
      url: "Url",
      key: "Key",
      location_geoname_id: "City",
    }
  );

  return response.data;
}

When a user clicks on a button there is another function that calls the create.

async function onClick() {
  try {
    await create();
    // Everything works fine when there are no errors
  } catch(error) {
    // Here error.response is undefined
  }
}

This is what i receive in the console.log, in the Network tab I can see the error status is 400 but even there there is no error response, I'am able to see the error response only on Postman.

enter image description here

Does anyone know what's wrong here ?

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

0

Please write await before creat(). so that if any any error occurs, catch with get that

async function onClick() {
    try {
      await create();
      // Everything works fine when there are no errors
    } catch (error) {
      // Here the error response is undefined
    }
  }
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