简体   繁体   English

React JS Map TypeError:无法读取未定义的属性“地图”

[英]React JS Map TypeError: Cannot read property 'map' of undefined

I am working on a MERN project and I am in the level of integration between frontend and backend with Axios.我正在做一个 MERN 项目,我处于前端和后端与 Axios 的集成级别。 However, I am facing some issues.但是,我面临一些问题。 I am facing this error in most of my list screens:我在大多数列表屏幕中都面临这个错误:

TypeError: Cannot read property 'map' of undefined 

This is the code I am using for the map function:这是我用于 map function 的代码:

      const carList = useSelector((state) => state.carList)


    useEffect(() => {
        if (!adminInfo) {
             history.push('/admin/login')
         }
        dispatch(listCars('', pageNumber))
    }, [
        dispatch,
        history,
        pageNumber,
    ])

        const { loading, error, cars, page, pages } = carList

return(
                                <tbody>
                                    {cars.map((car) => (
                                        <tr key={car._id}>
                                            <td>{car.companyId}</td>
                                            <td>{car.carPlate}</td>
                                            <td>{car.carModel}</td>
                                            <td>{car.color}</td>
                                            <td>{car.totalMileage}</td>
                                            <td>{car.status}</td>
                                            <td>{car.registrationDate}</td>
                                        </tr>
                                    ))}
                                </tbody>
)

Axios Code: Axios 代码:

export const listCars = (keyword = '', pageNumber = '') => async (
    dispatch
) => {
    try {
        dispatch({ type: CAR_LIST_REQUEST })
 
        const { data } = await axios.get(
            `http://localhost:5000/api/car/find-all-cars`
        )
 
        dispatch({
            type: CAR_LIST_SUCCESS,
            payload: data,
        })
    } catch (error) {
        dispatch({
            type: CAR_LIST_FAIL,
            payload:
                error.response && error.response.data.message
                    ? error.response.data.message
                    : error.message,
        })
    }
}

Reducers Code:减速机代码:

export const carListReducer = (state = { cars: [] }, action) => {
    switch (action.type) {
        case CAR_LIST_REQUEST:
            return { loading: true, cars: [] }
        case CAR_LIST_SUCCESS:
            return {
                loading: false,
                cars: action.payload.cars,
                pages: action.payload.pages,
                page: action.payload.page,
            }
        case CAR_LIST_FAIL:
            return { loading: false, error: action.payload }
        default:
            return state
    }
}

The idea is to loop through cars inside the DB and display them.这个想法是遍历数据库内的汽车并显示它们。 Also, this problem is occurring in every map function whether cars or something else.此外,这个问题发生在每个 map function 无论是汽车还是其他东西。 FYI, the API is tested with Postman and works fine.仅供参考,API 已经过 Postman 测试并且工作正常。

Make sure that the API data has been fetched before you attempt to loop through and render it.确保在尝试循环和渲染之前已获取 API 数据。 Try something like this:尝试这样的事情:

 {cars ? cars.map((car) => (
           <tr key={car._id}>
               <td>{car.companyId}</td>
               <td>{car.carPlate}</td>
               <td>{car.carModel}</td>
               <td>{car.color}</td>
               <td>{car.totalMileage}</td>
               <td>{car.status}</td>
               <td>{car.registrationDate}</td>
           </tr>
                  )) : ''}

I don't know if this is an answer or not but it solved the problem for me.我不知道这是否是一个答案,但它为我解决了问题。 So, initially in Axios request, it was retrieving the data in payload.因此,最初在 Axios 请求中,它正在检索有效负载中的数据。 But, I specified what's in payload exactly to be saved and that worked for me.但是,我指定了要保存的有效载荷中的内容,这对我有用。

dispatch({
    type: CAR_LIST_SUCCESS,
    payload: data.car,
})

This worked for me just fine这对我来说很好

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 React js TypeError:无法读取未定义的属性“地图” - React js TypeError: Cannot read property 'map' of undefined 类型错误:无法读取未定义的属性“地图”。 反应 js - TypeError: Cannot read property 'map' of undefined. React js TypeError:无法读取未定义的React Js的属性“ map” - TypeError: Cannot read property 'map' of undefined React Js TypeError:无法读取未定义的属性“地图” - 反应js - TypeError: Cannot read property 'map' of undefined - react js react js-× TypeError:无法读取未定义的属性“映射” - react js-× TypeError: Cannot read property 'map' of undefined TypeError:无法读取 react.js 中未定义的属性“地图” - TypeError: Cannot read property 'map' of undefined in react.js React.js 错误:TypeError:无法读取未定义的属性“地图” - React.js error :TypeError: Cannot read property 'map' of undefined TypeError:无法读取未定义React.js的属性“ map” - TypeError: Cannot read property 'map' of undefined React.js TypeError:无法读取React.js上未定义的属性&#39;map&#39; - TypeError: Cannot read property 'map' of undefined on React.js 未捕获的类型错误:无法读取未定义的属性“地图”-React js - Uncaught TypeError: Cannot read property 'map' of undefined - React js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM