React-native-async-storage 终止应用程序后无法识别时刻 toDate()

React-native-async-storage doesn't recognize moment toDate() after killing app

我正在实施“添加收藏夹”并将这些项目存储在 async-storage 与 react-redux 中。添加并从“主页”选项卡切换到“收藏夹”选项卡后,它工作正常。但是我在关闭应用程序并转到“收藏夹”选项卡后收到错误“date.toDate 不是函数”。

有什么建议和解决办法吗?非常感谢!

组件

      <CardInfo
      key={data.id.toString()}
      promotionToDate={moment(data.expireDate.toDate()).format('lll')}
      />

动作

export const getPromotionsAction = () => async dispatch => {
  dispatch({type: GET_PROMOTION});

  const response = await firebase.firestore().collection('xxxx').get();
  const values = [];
  response.docs.forEach(res => {
    values.push(res.data());
  });
  dispatch({type: GET_PROMOTION, payload: values});
  return values;
};

export const addFavoriteAction = fav => dispatch => {
  dispatch({
    type: ADD_PROMOTION_TO_FAVORITE_LIST,
    payload: fav,
  });
};

商店

// import rootReducer from '../redux';
import thunk from 'redux-thunk';
import PromotionReducer from './reducers';
import {persistStore, persistReducer} from 'redux-persist';
import AsyncStorage from '@react-native-async-storage/async-storage';
import {createStore, combineReducers, applyMiddleware} from 'redux';
import {logger} from 'redux-logger';

const persistConfig = {
  key: 'root',
  storage: AsyncStorage,
  whitelist: ['setPromotionToFavorites'],
};

const rootReducer = combineReducers({
  PromotionReducers: persistReducer(persistConfig, PromotionReducer),
});

export const store = createStore(rootReducer, applyMiddleware(thunk, logger));
export const persistor = persistStore(store);
export type RootStateType = ReturnType<typeof store.getState>;

已注明 我正在使用 Firestore,Momentjs enter image description here

JavaScript中的函数和对象都是引用类型,JavaScript中几乎所有东西都是对象,除了六个不是对象的东西:null 、未定义、字符串、数字、布尔值和符号。

当我们查看 AsyncStorage 时,如果我们必须将值存储在 AsyncStorage 中,那么 我们必须解析字符串数据类型中的数据.

当时使用JSON.stringify将对象转成字符串,函数无法转成字符串.

并且我们将 redux-persist 与 AsyncStorage 一起使用,后者通过 JSON.stringify 将对象存储到其中。

总而言之,我们必须确保通过返回函数的值将函数转换为 JSON 对象