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

182
Visualizações
Axios.all() POST request is not catching errors

I am creating a sign up page in react.js that requires two different axios post requests. I put both of the them in axios.all(). This is my code:

axios
  .all([
    axios.post(
      "https://api.chatengine.io/users/",
      { username: userName, secret: password }, // Body object
      { headers: authObject } // Headers object
    ),
    axios.post("http://localhost:4000/register", {
      email: email,
      password: password,
      username: userName,
    }),
  ])
  .then((res) => {
    if (res.data === "User With Email Already Exists") {
      setError("User With Email Already Exists");
      console.log(res);
    } else {
      navigate("/login");
    }
  });

For the most part it works completely fine. However, when a user signs up and puts in an email that already exists in my database, it does not throw the error that says "User with email already exists." It just does nothing. In the console it will say Failed to load resource: the server responded with a status of 400 () and Uncaught (in promise) Error: Request failed with status code 400. So there is clearly an error and the error message I have set does not display. When I had two separate axios calls it was able to display the error message with no issue, so what changed? Any help would be appreciated!

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

0

anything other than 2xx responses goes to "catch" in axios library. your api is responding with 400 so what you can do is:

axios
  .all([
    axios.post(
      "https://api.chatengine.io/users/",
      { username: userName, secret: password }, // Body object
      { headers: authObject } // Headers object
    ),
    axios.post("http://localhost:4000/register", {
      email: email,
      password: password,
      username: userName,
    }),
  ])
  .then((res) => {
      navigate("/login");
  })
  .catch((e) => {
      if (e.response?.data === "User With Email Already Exists") {
          setError("User With Email Already Exists");
          console.log(e):
      }
  });
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