使用redux-persist清除后,不定义不是对象

时间:2018-01-03 21:26:34

标签: react-native redux-persist

我将expires令牌存储到redux-persist中进行身份验证。我用过persistor.purge();在商店重置redux状态。现在,当我加载它时,我不断收到下面的错误消息。我不知道如何解决这个问题?

Unhandled promise rejection: TypeError: undefined is not an object (evaluating 'action.payload.auth')]
* reducers/auth_reducer.js:11:45 in default
- node_modules/redux/lib/combineReducers.js:133:36 in combination
- node_modules/redux-persist/lib/persistReducer.js:117:39 in <unknown>
- node_modules/redux/lib/createStore.js:178:36 in dispatch
- node_modules/redux-persist/lib/persistStore.js:100:21 in rehydrate
- node_modules/redux-persist/lib/persistReducer.js:85:27 in <unknown>
- node_modules/promise/setimmediate/core.js:37:14 in tryCallOne
- node_modules/promise/setimmediate/core.js:123:25 in <unknown>
- node_modules/react-native/Libraries/Core/Timers/JSTimers.js:154:6 in _callTimer
- node_modules/react-native/Libraries/Core/Timers/JSTimers.js:202:17 in _callImmediatesPass
- node_modules/react-native/Libraries/Core/Timers/JSTimers.js:470:11 in callImmediates
- node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:275:4 in __callImmediates
- node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:140:6 in <unknown>
- node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:262:6 in __guard
- node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:139:17 in flushedQueue

这是我的商店代码:

import { createStore, applyMiddleware, compose } from 'redux';
import ReduxThunk from 'redux-thunk';
import { AsyncStorage } from 'react-native';
import { persistStore, persistCombineReducers } from 'redux-persist';
//  import Reactotron from 'reactotron-react-native';
// import { PersistGate } from 'redux-persist/lib/integration/react';
import reducers from '../reducers';

const config = {
  key: 'root',
  storage: AsyncStorage,
  whitelist: ['momentDuration', 'auth', 'storybook'],
};

//
config.debug = true;
const persistReducer = persistCombineReducers(config, reducers);

export default function configureStore() {
  const store = createStore(
    persistReducer,
    {},
    compose(applyMiddleware(ReduxThunk)),
  );
  //  add a .purge() below;
  const persistor = persistStore(store);
  //  persistor.purge();
  return { persistor, store };
}

在我的减速机中我正在使用

case 'persist/REHYDRATE':
  return ({ tokenExpires: action.payload.auth.tokenExpires } || {});

1 个答案:

答案 0 :(得分:1)

你尝试过类似的东西吗?

case 'persist/REHYDRATE':
  return ({ tokenExpires:  action.payload && action.payload.auth && action.payload.auth.tokenExpires } || {});

此外,请确保您的动作创建者没有任何拼写错误,我希望它能帮助