为了账号安全,请及时绑定邮箱和手机立即绑定

Uncaught TypeError: _this.props.data.map is not a

Uncaught TypeError: _this.props.data.map is not a

蝴蝶刀刀 2023-02-17 15:42:03
我不知道我试图从 mongo 映射对象数组的组件发生了什么,但是它没有向 printInventory 显示任何内容,但是如果我调用 this.props.inventory 并且它需要所有数据!怎么了? printInventory = () => {    this.props.inventory.map((inventory) => {        return (            <CardInventario                cardTitle={inventory.name}                quantity={inventory.quantity}                description={inventory.price}            />        )    })}在这些功能中。接下来我将展示动作和减速器:inventoryReducer:import {TAKE_INVENTORY} from '../../types/inventoryTypes';const INITIAL_STATE ={    inventory: []}function invetoryReducer(state = INITIAL_STATE,action){    switch (action.type) {        case TAKE_INVENTORY:            return {...state, inventory: action.payload}            break;        default:            return state;            break;    }}export default invetoryReducer;这是 inventoryActions:import axios from 'axios'import { TAKE_INVENTORY } from '../../types/inventoryTypes'export const takeInventory = () => async (dispatch) =>{    const res = await axios.get('http://localhost:3001/inventory')        dispatch({            type: TAKE_INVENTORY,            payload: res.data        })    }
查看完整描述

1 回答

?
胡说叔叔

TA贡献1804条经验 获得超8个赞

首先,请使用正确的映射。inventoryReducer 不是您要映射的那个。该对象内的库存是您想要的。


const mapStateToProps = (reducers) => {

    return reducers.inventoryReducer.inventory;

}

另外如果在 this.props.inventory 中获取到数据,应该与重复键有关 请尝试以下操作


printInventory = () => {

    this.props.inventory.map((inventory, index) => {

        return (

            <CardInventario

                key={index}

                cardTitle={inventory.name}

                quantity={inventory.quantity}

                description={inventory.price}

            />

        )

    })

}

如果你没有 id,可以使用索引代替(虽然不推荐)


printInventory = () => {

    this.props.inventory.map((inventory) => {

        return (

            <CardInventario

                key={inventory.id}

                cardTitle={inventory.name}

                quantity={inventory.quantity}

                description={inventory.price}

            />

        )

    })

}


查看完整回答
反对 回复 2023-02-17
  • 1 回答
  • 0 关注
  • 168 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号