我将我的react应用程序升级到18版,然后我将从7.2.0升级到8版。
Requests.js Uncaught :无法读取空属性(读取‘数据’)
当我登录时,我会从响应中获取数据,但它不会保存在我的商店中。
import { createStore, applyMiddleware } from "redux";
import { persistStore, persistReducer } from "redux-persist";
import storage from "redux-persist/lib/storage";
import thunk from "redux-thunk";
import reducer from "../reducers/combiner";
const persistConfig = {
key: "app",
storage,
};
const persistedReducer = persistReducer(persistConfig, reducer);
const store = createStore(
persistedReducer,
applyMiddleware(thunk)
);
const persistor = persistStore(store);
export { store, persistor };我的依赖关系:
"dependencies": {
"@reduxjs/toolkit": "^1.8.2",
"bootstrap": "^5.1.3",
"crypto-js": "^4.1.1",
"js-base64": "^3.7.2",
"leaflet": "^1.8.0",
"leaflet-routing-machine": "^3.2.12",
"node-sass": "^7.0.1",
"react": "^18.1.0",
"react-dom": "^18.1.0",
"react-leaflet": "^4.0.0",
"react-redux": "^8.0.2",
"react-router-dom": "^6.3.0",
"react-scripts": "^5.0.1",
"redux": "^4.2.0",
"redux-persist": "^6.0.0",
"redux-thunk": "^2.4.1",
"reselect": "^4.1.5",
"typescript": "^4.7.2",
"uuid": "^3.4.0"},
谢谢你提前..。
这是我使用数据的地方:
useEffect(() => {
const authKey = localStorage.getItem("auth");
if (authKey === null) {
navigate("/login");
} else {
if (window.innerWidth <= 767) {
window.scrollTo({ top: 600, behavior: "smooth" });
}
if (userInfo !== null && userInfo.data && userInfo.data.order_id !== 0) {
navigate("/driver");
} else if (latLon.lat === 35.6892 && latLon.lng === 51.389) {
dispatch(sendToMapIR(latLon.lat, latLon.lng));
dispatch(deleteRestAddress());
}
}
}, []);这就是我的行动:
export const newRequest = (
userId,
lat,
lng,
address,
pickedItems,
total,
checked,
restAddress
) => {
return async dispatch => {
dispatch({ type: NEW_REQUSET_WAITING });
const result = await fetchNewRequest(
userId,
lat,
lng,
address,
pickedItems,
total,
checked,
restAddress
);
if (result.success) {
return dispatch({
type: NEW_REQUSET,
payload: result.data
});
} else {
return dispatch({
type: NEW_REQUSET_FAILED
});
}
};
};发布于 2022-06-09 15:13:51
我不知道userInfo对象是什么样子,但也许使用可选链可能会有所帮助:
if (userInfo !== null && userInfo?.data && userInfo?.data.order_id !== 0) {
navigate("/driver");
}如果没有,那么尝试console.log userInfo和userInfo.data并检查它是如何的。
https://stackoverflow.com/questions/72544059
复制相似问题