svrcekmichal / redux-axios-middleware

Redux middleware for fetching data with axios HTTP client
MIT License
920 stars 96 forks source link

Possible Unhandled Promise Rejection (id: 0) #75

Open dilipmukkapati opened 6 years ago

dilipmukkapati commented 6 years ago

Hi Guys thanks for the middleware library its really helpful.

Am facing issue when I receive success response from the server.

_FAIL works absolutely fine without error. But,

_SUCCESS is called and response is received as expected, but am getting a warning saying "possible unhandled promise Rejection (id: 1) TypeError: null is not a object (evaluating 'action.type') getStateForAction)"

Is it something you already faced, because promise is generated only by the middleware library for the action am performing.

Here is the config info:

const client = axios.create({ //all axios can be used, shown in axios documentation
    baseURL: 'http://192.168.5.10/CoreServices/',
    responseType: 'json'
});

const options = {
    interceptors: {
        request: [{
            success: ({getState, dispatch, getSourceAction}, req) => {
                if (getState() && getState().sessionReducer && getState().sessionReducer.token) {
                    req.headers['Authorization'] = 'Bearer ' + getState().sessionReducer.token
                }
                dispatch({
                    type: SHOW_LOADER
                });

                return req
            },
            error: ({getState, dispatch, getSourceAction}, req) => {
                dispatch({
                    type: HIDE_LOADER
                });

                return Promise.reject(req);
            }
        }],
        response: [{
            success: ({getState, dispatch, getSourceAction}, res) => {
                store.dispatch({
                    type: HIDE_LOADER
                });
                return res;
            },
            error: ({getState, dispatch, getSourceAction}, res) => {
                store.dispatch({
                    type: HIDE_LOADER
                });
                return Promise.reject(res);
            }
        }]
    }
};

switch (action.type) {
    case `${LOGIN}`:
        return {...state}
        break;
    case `${LOGIN}_SUCCESS`:
        return {...state, isLoggedIn: true}
        break;
    case `${LOGIN}_FAIL`:
        console.log('login failed!');
        if(action.error &&
            action.error.response &&
            action.error.response.data &&
            action.error.response.data.errors &&
            Array.isArray(action.error.response.data.errors) &&
            action.error.response.data.errors.length > 0) {
            return {...state, isLoggedIn: false, error: action.error.response.data.errors[0].message}
        } else {
            return {...state, error: 'test error from backend'}
        }
        break;
    case RESET_AUTHENTICATION_ERROR:
        return {...state, error: null}
        break;
    default:
        return {...state}
}

Please let me know if you have some info about this.