如何解决“未处理的拒绝(TypeError):undefined不是对象(评估'err.response.data')”

时间:2019-06-01 15:33:57

标签: reactjs redux

我正在使用redux作为状态管理器来构建React应用。所有操作在桌面上都可以正常运行,但是当我尝试在iOS上使用该应用程序时就会崩溃。 例如,我有一个登录页面,它可以在桌面Web浏览器上正常运行,但在ios上却出现此错误-未处理的拒绝(TypeError):undefined不是对象(评估'err.response.data')

我在下面附加了我的redux存储文件

最初,我认为redux开发工具存在错误,但是即使删除了该行代码,它仍然崩溃

  createStore,
  applyMiddleware,
  compose
} from 'redux';
import thunk from 'redux-thunk';
import rootReducer from './reducers/index'
const middleware = [thunk];
const initialState = {};



const getComposeEnhancers = () => {
  if (window.navigator.userAgent.includes('Chrome')) {
    return compose(
      applyMiddleware(...middleware), window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()
    );
  } else

    return compose(applyMiddleware(...middleware));
};



const store = createStore(rootReducer,
  initialState,
  getComposeEnhancers()
);

export default store;
// This is the authActions file, responsible for registering and login
import axios from 'axios';
import setAuthToken from '../utils/setAuthToken';
import {GET_ERRORS, SET_CURRENT_USER} from './types';
import jwt_decode from 'jwt-decode';
const url = 'http://localhost:5000/api';

//register

export const registerUser = (userData, history) => dispatch => {
  axios.post(`${url}/auth/register`, userData)
       .then(res=>{history.push('/login')

     }).catch(err=> dispatch({
       type: GET_ERRORS,
       payload: err.response.data
     }))
}

//login 
export const loginUser = (userData)=> dispatch=>{
  axios.post(`${url}/auth/login`, userData)
        .then(res=>{
          //save to local storage
          const {token} = res.data;
          //set token to localstorage
          localStorage.setItem('jwt_token', token);
          //set token to auth header
          setAuthToken(token);
          //decode token to get userData
          const decoded = jwt_decode(token);
          //set current user
          dispatch(setCurrentUser(decoded))

        }).catch(err=> dispatch({
          type: GET_ERRORS,
          payload: err.response.data
        }))
}

//set logged in user

export const setCurrentUser = (decoded)=>{
  return {
    type: SET_CURRENT_USER,
    payload: decoded
  }
}


//log out user
export const logoutUser=()=>dispatch=>{
//rm jwt_token
localStorage.removeItem('jwt_token');
//rm auth header
setAuthToken(false);
//set setCurrentUser to {}
dispatch(setCurrentUser({}))


}```

0 个答案:

没有答案