Javascript 未捕获类型错误:无法读取属性';地图';Reactjs-redux中未定义的
我正在与MongoDB、Express、React、Node(MERN)项目合作。在更改redux文件以实现从材质UI/core加载效果后,我遇到了“无法读取未定义的属性“映射”的问题 我曾尝试通过useSelector以不同的方式访问数据,甚至使用shallowEqual方法。我还尝试调用DashBoardAdmin中的getStudents()。同时还尝试使用useEffect分派(getStudents())使用dependencies数组。到目前为止,一切都不起作用。然后尝试在chrome的inspect部分进行调试,在第一次重新加载页面时,我发现在action.payload上碰巧从后端获取了数据,但它无法填充到整个状态中。这可能是useSelector获取空数组的原因&提供“无法读取未定义的属性“map” 我想,在状态中引入一个对象之后,在reducer的students.js文件之后出现了一些问题。我正在尽力调试 我的index.js文件:Javascript 未捕获类型错误:无法读取属性';地图';Reactjs-redux中未定义的,javascript,reactjs,redux,react-redux,react-router-dom,Javascript,Reactjs,Redux,React Redux,React Router Dom,我正在与MongoDB、Express、React、Node(MERN)项目合作。在更改redux文件以实现从材质UI/core加载效果后,我遇到了“无法读取未定义的属性“映射”的问题 我曾尝试通过useSelector以不同的方式访问数据,甚至使用shallowEqual方法。我还尝试调用DashBoardAdmin中的getStudents()。同时还尝试使用useEffect分派(getStudents())使用dependencies数组。到目前为止,一切都不起作用。然后尝试在chrom
import React from "react";
import ReactDOM from "react-dom";
import "./Styles/index.css";
import App from "./App";
import { Provider } from "react-redux";
import { createStore, applyMiddleware, compose } from "redux";
import thunk from "redux-thunk";
import { reducers } from "./redux/reducers/index";
const composeEnhancers =
typeof window === "object" && window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__
? window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({})
: compose;
const enhancer = composeEnhancers(compose(applyMiddleware(thunk)));
const store = createStore(reducers, enhancer);
ReactDOM.render(
<Provider store={store}>
<App />
</Provider>,
document.getElementById("root")
);
import { FETCH_STUDENTS } from "../constants/actionTypes";
export default (students = [], action) => {
switch (action.type) {
case FETCH_STUDENTS:
return action.payload;
default:
return students;
}
};
My API index.js文件:
import axios from "axios";
const API = axios.create({ baseURL: "http://localhost:5000" });
API.interceptors.request.use((req) => {
if (localStorage.getItem("account")) {
req.headers.Authorization = `Bearer ${
JSON.parse(localStorage.getItem("account")).token
}`;
}
return req;
});
export const fetchStudents = () => API.get("/students-info");
我的reducers by students.js,最好的猜测是这里出了问题,或者是在我包含isLoading之后开始的:
import { FETCH_STUDENTS, START_LOADING, END_LOADING } from "../constants/actionTypes";
function students(state = { isLoading: true, students: [] }, action) {
switch (action.type) {
case START_LOADING:
return { ...state, isLoading: true };
case END_LOADING:
return { ...state, isLoading: false };
case FETCH_STUDENTS:
return { ...state, students: action.payload.data };
default:
return state;
}
}
export default students;
index.js文件:
import { combineReducers } from "redux";
import students from "./students";
import auth from "./auth";
export const reducers = combineReducers({ students, auth });
我得到的错误有:
Uncaught TypeError: Cannot read property 'map' of undefined
at DashBoardAdmin (DashBoardAdmin.js:51)
at renderWithHooks (react-dom.development.js:14985)
at updateFunctionComponent (react-dom.development.js:17356)
at beginWork (react-dom.development.js:19063)
at HTMLUnknownElement.callCallback (react-dom.development.js:3945)
at Object.invokeGuardedCallbackDev (react-dom.development.js:3994)
at invokeGuardedCallback (react-dom.development.js:4056)
at beginWork$1 (react-dom.development.js:23964)
at performUnitOfWork (react-dom.development.js:22776)
at workLoopSync (react-dom.development.js:22707)
at renderRootSync (react-dom.development.js:22670)
at performSyncWorkOnRoot (react-dom.development.js:22293)
at react-dom.development.js:11327
at unstable_runWithPriority (scheduler.development.js:468)
at runWithPriority$1 (react-dom.development.js:11276)
at flushSyncCallbackQueueImpl (react-dom.development.js:11322)
at flushSyncCallbackQueue (react-dom.development.js:11309)
at batchedUpdates$1 (react-dom.development.js:22387)
at Object.notify (Subscription.js:19)
at Subscription.notifyNestedSubs (Subscription.js:90)
at Subscription.handleChangeWrapper (Subscription.js:95)
at Object.dispatch (redux.js:297)
at dispatch (<anonymous>:3856:17)
at index.js:11
at dispatch (redux.js:659)
at studentAction.js:35
需要获得另一种实现isLoading的方法,或者启动加载/结束加载到UI调度在看到你的app.js后,我看到你在使用thunk,我认为你必须使用函数,而不是直接传递对象 此外,我还添加了一个操作以获取错误,以防万一 studentAction.js students.js 你的app.js和index.js对我来说是正确的 在Admin.js中,您可以使用此条件
import { useSelector } from "react-redux";
const DashBoardAdmin = () => {
const { students, isLoading, error } = useSelector((state) => state.students);
return (
<>
{error === "" && (
<div className="padding-grid">
...
{isLoading || !students.length > 0 ? (
<h1>loading...</h1>
) : (
<div>
{students.map((stu) => (
<p key={stu.id}>{JSON.stringify(stu)}</p>
))}
</div>
)}
...
</div>
)}
</>
);
};
export default DashBoardAdmin;
从“react redux”导入{useSelector};
const仪表板管理员=()=>{
const{students,isload,error}=useSelector((state)=>state.students);
返回(
{错误===“”&&(
...
{isLoading | |!students.length>0(
加载。。。
) : (
{学生地图((stu)=>(
{JSON.stringify(stu)}
))}
)}
...
)}
);
};
导出默认仪表板管理员;
快速修复:如果学生
是未定义的
{(学生| |[]),则使用空数组。map((stu)=>(
@LindaPaiste Yes此方法帮助我至少看到UI部分,错误停止在屏幕上弹出,但数据仍然没有更新到redux存储。感谢上述方法。如果您能够解决使用数据填充状态的任何问题,将不胜感激。我怀疑“students:action.payload.data”在reducer中应该改为“students:action.payload”。但这取决于API响应的格式。@LindaPaiste yes尝试了它,但仍然返回空数组。但是我通过实现导出默认值(students=[],action)=>{switch(action.type)来获取数据{case FETCH_STUDENTS:return action.payload;默认值:return STUDENTS;}};但同样,无法实现START_加载和END_加载。任何关于添加START_加载的建议我已经创建了用于呈现app.js的默认index.js文件。下一步将添加到上面的原始查询中。这与您所建议的类似。我通过实现以下方式获取数据:导出默认值(学生=[],操作)=>{switch(action.type){case FETCH_STUDENTS:return action.payload;default:return STUDENTS;}};请帮助我在STUDENTS.js文件中实现START_加载或isLoading替代方法shi@Clinto_92_Abraham我刚刚编辑了我的答案,希望对您有所帮助
Uncaught TypeError: Cannot read property 'map' of undefined
at DashBoardAdmin (DashBoardAdmin.js:51)
at renderWithHooks (react-dom.development.js:14985)
at updateFunctionComponent (react-dom.development.js:17356)
at beginWork (react-dom.development.js:19063)
at HTMLUnknownElement.callCallback (react-dom.development.js:3945)
at Object.invokeGuardedCallbackDev (react-dom.development.js:3994)
at invokeGuardedCallback (react-dom.development.js:4056)
at beginWork$1 (react-dom.development.js:23964)
at performUnitOfWork (react-dom.development.js:22776)
at workLoopSync (react-dom.development.js:22707)
at renderRootSync (react-dom.development.js:22670)
at performSyncWorkOnRoot (react-dom.development.js:22293)
at react-dom.development.js:11327
at unstable_runWithPriority (scheduler.development.js:468)
at runWithPriority$1 (react-dom.development.js:11276)
at flushSyncCallbackQueueImpl (react-dom.development.js:11322)
at flushSyncCallbackQueue (react-dom.development.js:11309)
at batchedUpdates$1 (react-dom.development.js:22387)
at Object.notify (Subscription.js:19)
at Subscription.notifyNestedSubs (Subscription.js:90)
at Subscription.handleChangeWrapper (Subscription.js:95)
at Object.dispatch (redux.js:297)
at dispatch (<anonymous>:3856:17)
at index.js:11
at dispatch (redux.js:659)
at studentAction.js:35
Warning: validateDOMNesting(...): <div> cannot appear as a child of <table>.
at div
at CircularProgress (http://localhost:4000/static/js/vendors~main.chunk.js:80761:23)
at WithStyles (http://localhost:4000/static/js/vendors~main.chunk.js:119309:31)
at table
at Table (http://localhost:4000/static/js/vendors~main.chunk.js:102171:23)
at WithStyles (http://localhost:4000/static/js/vendors~main.chunk.js:119309:31)
at div
at StyledComponent (http://localhost:4000/static/js/vendors~main.chunk.js:119080:28)
at div
at ScrollBar (http://localhost:4000/static/js/vendors~main.chunk.js:231982:5)
at div
at Paper (http://localhost:4000/static/js/vendors~main.chunk.js:94231:23)
import { FETCH_STUDENTS } from "../constants/actionTypes";
export default (students = [], action) => {
switch (action.type) {
case FETCH_STUDENTS:
return action.payload;
default:
return students;
}
};
import {
FETCH_STUDENTS,
START_LOADING,
END_LOADING,
} from "../constants/actionTypes";
import * as api from "../api";
const startLoading = () => {
return {
type: START_LOADING,
};
};
const fetchStudents = (data) => {
return {
type: FETCH_STUDENTS,
data,
};
};
const endLoading = (error) => {
return {
type: END_LOADING,
error,
};
};
export const getStudents = () => {
return async (dispatch) => {
dispatch(startLoading());
try {
const { data } = await api.fetchStudents()
dispatch(fetchStudents(data))
} catch (error) {
dispatch(endLoading(error.message));
}
};
};
import {
FETCH_STUDENTS,
START_LOADING,
END_LOADING,
} from "../../constants/actionTypes";
const initialState = {
isLoading: false,
error: "",
students: [],
};
function students(state = initialState, action) {
switch (action.type) {
case START_LOADING:
return { ...state, isLoading: true };
case END_LOADING:
return { ...state, isLoading: false, students: [], error: action.error };
case FETCH_STUDENTS:
return {
...state,
isLoading: false,
students: action.data,
};
default:
return state;
}
}
export default students;
import { useSelector } from "react-redux";
const DashBoardAdmin = () => {
const { students, isLoading, error } = useSelector((state) => state.students);
return (
<>
{error === "" && (
<div className="padding-grid">
...
{isLoading || !students.length > 0 ? (
<h1>loading...</h1>
) : (
<div>
{students.map((stu) => (
<p key={stu.id}>{JSON.stringify(stu)}</p>
))}
</div>
)}
...
</div>
)}
</>
);
};
export default DashBoardAdmin;