redux-axios-middleware icon indicating copy to clipboard operation
redux-axios-middleware copied to clipboard

TypeError: Cannot read property 'cancelToken' of undefined

Open itsazzad opened this issue 7 years ago • 11 comments

const middlewareConfig = {
    interceptors: {
        request: [
            {
                success: function ({getState, dispatch, getSourceAction}, req) {
                    console.error(1, req);
                },
                error: function ({getState, dispatch, getSourceAction}, error) {
                    console.error(2, error);
                }
            }
        ],
        response: [
            {
                success: function ({getState, dispatch, getSourceAction}, req) {
                    console.error(3, req);
                },
                error: function ({getState, dispatch, getSourceAction}, error) {
                    console.error(4, error);
                }
            }
        ]
    }
};
const middleware = [
    multiClientMiddleware(clients, middlewareConfig),
    routerMiddleware(history)
];

export default createStore(
    reducers,
    composeEnhancers(
        applyMiddleware(...middleware)
    )
)
TypeError: Cannot read property 'cancelToken' of undefined
    at throwIfCancellationRequested (index.js?v=:72)
    at dispatchRequest (index.js?v=:84)
screen shot 2018-04-23 at 3 17 55 pm screen shot 2018-04-23 at 3 19 33 pm screen shot 2018-04-23 at 3 21 42 pm screen shot 2018-04-23 at 3 23 48 pm

itsazzad avatar Apr 23 '18 09:04 itsazzad

@itsazzad I met this problem too, have you got some solutions?

liyuanqiu avatar Jul 18 '18 07:07 liyuanqiu

+1,I met too, @liyuanqiu, Have you solved it?

givingwu avatar Jul 24 '18 16:07 givingwu

Look the Comments

axios.interceptors.request.use(function(config) {
  console.log('request => config ====================================');
  console.log(config);
  console.log('request => config ====================================');

  // if u add new Chainable promise or other interceptor
  // You have to return `config` inside of a rquest
  // otherwise u will get a very confusing error
  // and spend sometime to debug it.
  return config;
}, function(error) {
  return Promise.reject(error);
});

givingwu avatar Jul 24 '18 16:07 givingwu

@Vuchan That's right, you must return req/error in those callback functions.

liyuanqiu avatar Oct 15 '18 08:10 liyuanqiu

If you write API.interceptors.request.use(config => { console.log(config) return config }) not forget return config

Wuruiyang avatar Aug 20 '19 07:08 Wuruiyang

@vuchan you are right after return it's working perfectly. Below is my working code,

axiosInstance.interceptors.request.use((request) => { this.setState({ showSpinner: true}); return requestHandler(request); }, (error) => { console.log('Interceptor error', error); this.setState({ showSpinner: false}); return Promise.reject(error); });

axiosInstance.interceptors.response.use((response) => {
  this.setState({ showSpinner: false});
  return successHandler(response);
}, (error) => {
  this.setState({ showSpinner: false});
  return errorHandler(error);
});

MartinJesu avatar Sep 18 '19 10:09 MartinJesu

Config should be returned at the end

oceceli avatar Feb 16 '20 13:02 oceceli

If you write API.interceptors.request.use(config => { console.log(config) return config }) not forget return config

thanks, a lot.

hardeath950 avatar Jul 15 '21 21:07 hardeath950

Look the Comments

axios.interceptors.request.use(function(config) {
  console.log('request => config ====================================');
  console.log(config);
  console.log('request => config ====================================');

  // if u add new Chainable promise or other interceptor
  // You have to return `config` inside of a rquest
  // otherwise u will get a very confusing error
  // and spend sometime to debug it.
  return config;
}, function(error) {
  return Promise.reject(error);
});

this saved my day. thanks :)

premk92 avatar Jul 30 '21 11:07 premk92

hi

GunelYusuf avatar Apr 23 '22 01:04 GunelYusuf

thanks,that's right ! bro

Janeonly300 avatar Dec 11 '22 07:12 Janeonly300