亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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 關注
  • 150 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號