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

我不知道我试图从 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

        })

    

}


蝴蝶刀刀
浏览 169回答 1
1回答

胡说叔叔

首先,请使用正确的映射。inventoryReducer 不是您要映射的那个。该对象内的库存是您想要的。const mapStateToProps = (reducers) => {&nbsp; &nbsp; return reducers.inventoryReducer.inventory;}另外如果在 this.props.inventory 中获取到数据,应该与重复键有关 请尝试以下操作printInventory = () => {&nbsp; &nbsp; this.props.inventory.map((inventory, index) => {&nbsp; &nbsp; &nbsp; &nbsp; return (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <CardInventario&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; key={index}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cardTitle={inventory.name}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; quantity={inventory.quantity}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; description={inventory.price}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; />&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; })}如果你没有 id,可以使用索引代替(虽然不推荐)printInventory = () => {&nbsp; &nbsp; this.props.inventory.map((inventory) => {&nbsp; &nbsp; &nbsp; &nbsp; return (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <CardInventario&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; key={inventory.id}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cardTitle={inventory.name}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; quantity={inventory.quantity}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; description={inventory.price}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; />&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; })}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript