简体   繁体   English

类型错误:无法使用 react-redux 读取未定义的属性“地图”

[英]TypeError: Cannot read property 'map' of undefined with react-redux

TypeError: Cannot read property 'map' of undefined TypeError:无法读取未定义的属性“地图”

I try a lot more, I cant solve this please help me guys I have my backend working but this is my problem, I am using redux, my map is working on useState mode but I need to use redux to upgrade my code and my knowledge but this is my problem我尝试了很多,我无法解决这个问题请帮助我,我的后端正在工作,但这是我的问题,我正在使用 redux,我的 map 正在使用 useState 模式,但我需要使用 Z7490FE17CAEC3273F2299DE65086 升级我的代码但这是我的问题

在此处输入图像描述

this is my HomeScreen.js这是我的 HomeScreen.js

import React, { useEffect } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { Row, Col } from 'react-bootstrap';
import Product from '../components/Product';
import { listProducts } from '../actions/productActions';

const HomeScreen = () => {
    const dispatch = useDispatch();

    const productList =useSelector(state => state.productList );
    const { loading, error, products } = productList;

    useEffect(() => {
        dispatch(listProducts())
    }, [dispatch]); 

    return (
        <>    
            <h1>Latest Products</h1>
            { loading ? (
                <h2>Loading...</h2>
            ) : error ? (
                <h3>{error}</h3>
            ) : (
            <Row>
                {products.map((product) => (
                    <Col key={product._id} sm={12} md={6} lg={4} xl={3}>
                        <Product product={product} />
                    </Col>
                ))}
            </Row>
            )}
        </>
    )
}

export default HomeScreen

this is my productReducers.js这是我的 productReducers.js

import { 
    PRODUCT_LIST_REQUEST,
    PRODUCT_LIST_SUCCESS,
    PRODUCT_LIST_FAIL
} from '../constants/productConstants';

export const productListReducer = (state = { products: [] }, action) => {
    switch(action.type) {
        case PRODUCT_LIST_REQUEST:
            return { loading: true, products: [] }
        case PRODUCT_LIST_SUCCESS:
            return { loading: false, product: action.payload }
        case PRODUCT_LIST_FAIL:
            return { loading: false, error: action.payload }
        default:
            return state
    }
}

this is my productAction.js这是我的 productAction.js

import axios from 'axios';
import { 
    PRODUCT_LIST_REQUEST,
    PRODUCT_LIST_SUCCESS,
    PRODUCT_LIST_FAIL
} from '../constants/productConstants';

export const listProducts = () =>  async (dispatch) => {
    try {
        dispatch({ type: PRODUCT_LIST_REQUEST })

        const { data } = await axios.get('/api/products')

        dispatch({
            type: PRODUCT_LIST_SUCCESS,
            payload: data
        })
    } catch (error) {
        dispatch({
            type: PRODUCT_LIST_FAIL,
            payload: error.response && error.response.data.message 
                ? error.response.data.message 
                : error.message,
        })
    }
}

this is my productConstants.js这是我的 productConstants.js

export const PRODUCT_LIST_REQUEST = 'PRODUCT_LIST_REQUEST';
export const PRODUCT_LIST_SUCCESS = 'PRODUCT_LIST_SUCCESS';
export const PRODUCT_LIST_FAIL = 'PRODUCT_LIST_FAIL';

this is my store.js这是我的 store.js

import { createStore, combineReducers, applyMiddleware } from 'redux';
import thunk from 'redux-thunk';
import { composeWithDevTools } from 'redux-devtools-extension';
import { productListReducer } from './reducers/productReducers';

const reducer = combineReducers({
    productList: productListReducer,
});

const initialState = {};

const middleware = [thunk];

const store = createStore(reducer, initialState, 
composeWithDevTools(applyMiddleware(...middleware)));


export default store;

There is a typo in the reducer.减速机有错别字。 it should be products , not product它应该是products ,而不是product

export const productListReducer = (state = { products: [] }, action) => {
    switch(action.type) {
        case PRODUCT_LIST_REQUEST:
            return { loading: true, products: [] }
        case PRODUCT_LIST_SUCCESS:
            return { loading: false, products: action.payload }   // <<< Here <<<<
        case PRODUCT_LIST_FAIL:
            return { loading: false, error: action.payload }
        default:
            return state
    }
}

Try printing the logs everywhere so that you can debug it easily.尝试在任何地方打印日志,以便您可以轻松调试它。

  1. print data inside your action.在您的操作中打印数据。 check if the intended data you are getting or not.检查您是否获得了预期的数据。
  2. if yes, go to reducer.如果是,go 到减速机。 Print the action.paylod and updated store's content before returning.在返回之前打印action.paylod和更新的商店内容。
  3. Now if both the cases are true, you have the data.现在,如果这两种情况都是正确的,那么您就有了数据。 Go to component and print the productList . Go 组件并打印productList Check if you have the data you need.检查您是否拥有所需的数据。

暂无
暂无

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

相关问题 错误:类型错误:无法读取 React-Redux 中未定义的属性“map” - Error: TypeError: Cannot read property 'map' of undefined in React-Redux TypeError:无法读取react-redux中未定义的属性“map” - TypeError: Cannot read property 'map' of undefined in react-redux 类型错误:无法读取未定义 React-Redux 的属性“地图” - TypeError: Cannot read property 'map' of undefined React-Redux 在执行 react-redux 项目时出现此错误。 错误:- TypeError:无法读取 React-Redux 项目中未定义的属性“地图” - Getting this error while doing react-redux project. Error:- TypeError: Cannot read property 'map' of undefined in React-Redux project React-Redux - 类型错误:无法读取未定义的属性(读取“地图”) - React-Redux - TypeError: Cannot read properties of undefined (reading 'map') React-Redux TypeError:无法读取未定义的属性“ setStatus” - React-Redux TypeError: Cannot read property 'setStatus' of undefined 未捕获的TypeError:无法读取react-redux中未定义的属性&#39;props&#39; - Uncaught TypeError: Cannot read property 'props' of undefined in react-redux TypeError:无法读取未定义的属性“操作”(React-Redux) - TypeError: Cannot read property 'actions' of undefined (React-Redux) React-Redux:无法读取未定义的属性“地图” - React-Redux: Cannot read property 'map' of undefined React-redux 在 TypeError 中渲染帖子时出错:无法读取未定义的属性“地图”但控制台具有所有帖子 - React-redux Getting error while rendering post in TypeError:Cannot read property 'map' of undefined but console having all posts
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM