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

236
Vistas
How to catch status = cancelled in axios?

I am currently running 2 servers:

  1. To serve the view using react which retrieves data from REST API built with express.
  2. To provide the REST API for the view.

Below is my action for logging a user in:

// Redux Action
export function loginUser(creds, role) {

  return dispatch => {
    // We dispatch requestLogin to kickoff the call to the API
    dispatch(requestLogin(creds));

    return axios.post(`${ROOT_URL}/login/${role}`, creds).then((response) => {
        console.log(response);

        if(response.status === 200) {
          // If login was successful, set the token in local storage
          localStorage.setItem('id_token', response.data);

          // Dispatch the success action
          dispatch(receiveLogin(response));

          return response;
        }
      }).catch(err => {
        // If there was a problem, we want to
        // dispatch the error condition
        dispatch(loginError(err.data));

        return err;
      });
  };
}

I purposely disconnected my database to catch errors and see what happens. So, this is what I can see in the terminal:

12:49:24 Project-0 Server is listening at port 3000
12:49:24 Project-0 Mongoose disconnected
12:49:24 Project-0 Mongoose connection error: MongoError: connect ECONNREFUSED 192.168.1.116:27017
12:49:34 Project-0 Wed, 13 Apr 2016 07:19:34 GMT express deprecated res.send(status): Use res.sendStatus(status) instead at app/index.js:61:7
12:49:34 Project-0 OPTIONS /login/admin Wed, 13 Apr 2016 07:19:34 GMT ::ffff:192.168.1.134 200 5.894
12:49:35 Project-0 POST /login/admin Wed, 13 Apr 2016 07:19:35 GMT ::ffff:192.168.1.134 - -

Now, when I submit the login form, status goes from pending to cancelled.

How can we catch this status using axios or do we have to write a mechanism for this in express itself?

Axios 0.10.0

Note: I couldn't tag axios as the tag doesn't exist and I can't create a new one.

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

use axios.isCancel()

.catch(err => { 
      if(axios.isCancel(err)) {
        // do something
      }
   }
)
over 4 years ago · Santiago Trujillo Denunciar

0

You actually catch errors(unsuccessful responses) in catch statement. You can for example log it to console like this:

.catch(err => {
        // If there was a problem, we want to
        // dispatch the error condition
        dispatch(loginError(err.data));
        console.log(err);
        return err;
      });

over 4 years ago · Santiago Trujillo Denunciar
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