(节点:3440)UnhandledPromiseRejectionWarning:TypeError:无法读取未定义的属性“ origin”

时间:2019-11-09 21:23:07

标签: javascript reactjs react-redux jestjs enzyme

在我的react应用程序中,我添加了一个中间件,该中间件检查令牌是否有效,如果不是,则清除本地存储并将人员重定向到登录页面,并且在我的商店index.js中,我在传递代码之前添加了该中间件。问题是当我运行测试时,我会得到

  

(节点:3440)UnhandledPromiseRejectionWarning:TypeError:无法读取未定义的属性“ origin”

我该如何解决?请帮助

这是我添加的代码

checkTokenMiddleware.js


const checkTokenMiddleware = () => next => action => {
  try {
    jwdDecoded(localStorage.token);
  } catch (error) {
    if (localStorage.token) {
      window.location.assign('/login');
    }
    localStorage.token = '';
  }
  next(action);
};

export default checkTokenMiddleware;

在商店目录中 index.js

import { composeWithDevTools } from 'redux-devtools-extension';
import thunk from 'redux-thunk';
import rootReducer from '../reducers';
import checkTokenMiddleware from '../middleware/checkTokenMiddleware';

const initialState = {};
const middleware = [checkTokenMiddleware, thunk];
const store = createStore(
  rootReducer,
  initialState,
  composeWithDevTools(applyMiddleware(...middleware)),
);

export default store;

0 个答案:

没有答案