首页 > 解决方案 > 对于单个产品:React TypeError: Cannot read property 'map' of undefined :

问题描述

这是单品数据。

const [single, setSingle]= useState([])

setSingle(res.data) (axios set state)


{
  "id": 135,
  "device_detail": [
    {
      "type": "high-speed",
      "brief": "dsf",
      "model": "ds",
      "serial": "ds"
    },
    {
      "type": "low-speed",
      "brief": "dfg",
      "model": "hgj",
      "serial": "dsf"
    }
  ],
  "device_name": "Handpieces",
  "practice_name": "dsf",
  "unit": "dsf",
  "street": "dsf",,
  "status": 1,
  "created_at": "2021-03-02 14:00:58.295738+00:00"
}

当我使用

{single.device_detail.map((subd,index)=>(
(index ? ', ' : '') + subd.type
 ))}   

它说 TypeError: Cannot read property 'map' of undefined :

但是当我为 device_detail 创建另一个状态,然后它的工作方式如下:

const [dd, setdd]= useState([])

setdd(res.data.device_detail) (axios set state)

但是,我这不是一个合适的解决方案。

任何想法?

标签: reactjsdjangodjango-rest-framework

解决方案


推荐阅读