简体   繁体   English

× TypeError: Cannot read properties of undefined (reading 'map') in reactjs

[英]× TypeError: Cannot read properties of undefined (reading 'map') in reactjs

hey guys i don't understand why am I getting this error when i want get api from api.Covid19 and it return a object that contain Countries array whenever i want map over Countries i get this error and if i get Countries api separately it work find:| hey guys i don't understand why am I getting this error when i want get api from api.Covid19 and it return a object that contain Countries array whenever i want map over Countries i get this error and if i get Countries api separately it work查找:| help me pls enter image description here请帮我在这里输入图片描述

getting from api从 api 获取

using context使用上下文

looping over data.Countries (this is problem section)循环 data.Countries (这是问题部分)

and the image below shows what api returns: object return by api下图显示了 api 返回的内容: object 返回 api

Issues问题

The initial data state is an object with no defined Countries property, so in the component data.countries is undefined and throws error when you attempt to map it.初始data state 是一个 object 没有定义的Countries属性,所以在组件中data.countries是未定义的,当你尝试 map 时会抛出错误。

The https://api.covid19api.com/summary endpoint does return an object with a Countries property. https://api.covid19api.com/summary端点确实返回了一个带有 Country 属性的Countries

 / 20211121231605 // https://api.covid19api.com/summary { "ID": "03b65afe-02ad-4835-abb5-9916c44bae85", "Message": "", "Global": { "NewConfirmed": 408370, "TotalConfirmed": 256947981, "NewDeaths": 5200, "TotalDeaths": 5144527, "NewRecovered": 0, "TotalRecovered": 0, "Date": "2021-11-22T06:09:08.91Z" }, "Countries": [ { "ID": "19a0f201-0385-42ee-bfec-cfe99666a87c", "Country": "Afghanistan", "CountryCode": "AF", "Slug": "afghanistan", "NewConfirmed": 32, "TotalConfirmed": 156896, "NewDeaths": 2, "TotalDeaths": 7365, "NewRecovered": 0, "TotalRecovered": 0, "Date": "2021-11-22T06:09:08.91Z", "Premium": { } }, ...

The solution seems to be to provide valid initial state in the provider for what you expect all consumers to render from:解决方案似乎是在提供程序中提供有效的初始 state 以供您期望所有消费者从中呈现:

const [data, setData] = useState({ Countries: [] });

Or just use null/checks/guard-clauses or Optional Chaining operator to protect against the null/undefined accesses.或者只使用 null/checks/guard-clauses 或 Optional Chaining 运算符来防止 null/undefined 访问。

data.Countries && data.Countries.map(country => (
  <h2 key={country.ISO2}>
    {country.Country}
  </h2>
))

... ...

data.Countries?.map(country => (
  <h2 key={country.ISO2}>
    {country.Country}
  </h2>
))

暂无
暂无

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

相关问题 Reactjs(Nextjs):无法读取未定义的属性(读取“地图”)Reactjs(Nextjs) - Reactjs(Nextjs): Cannot read properties of undefined (reading 'map') Reactjs(Nextjs) Sidebar.jsx:20 Uncaught TypeError: Cannot read properties of undefined (reading &#39;map&#39;) in firebase9 reactjs - Sidebar.jsx:20 Uncaught TypeError: Cannot read properties of undefined (reading 'map') in firebase9 reactjs TypeError: Cannot read properties of undefined (reading 'map') ReactJs 从文件中获取数据 - TypeError: Cannot read properties of undefined (reading 'map') ReactJs Getting data from a file 在 reactjs TypeError 中出现错误:无法读取未定义的属性(读取“过滤器”) - Getting error in reactjs TypeError: Cannot read properties of undefined (reading 'filter') ReactJS/Firebase:TypeError:无法读取未定义的属性(读取“indexOf”) - ReactJS/Firebase: TypeError: Cannot read properties of undefined (reading 'indexOf') ReactJs:未捕获的类型错误:无法读取未定义的属性(读取“0”) - ReactJs: Uncaught TypeError: Cannot read properties of undefined (reading '0') ReactJS Redux:未捕获的类型错误:无法读取未定义的属性(读取“类型”) - ReactJS Redux: Uncaught TypeError: Cannot read properties of undefined (reading 'type') 未捕获的类型错误:无法读取未定义的属性(读取“名称”)-ReactJS - Uncaught TypeError: Cannot read properties of undefined (reading 'name') - ReactJS TypeError:无法读取未定义的属性(读取“查找”)ReactJS - TypeError: Cannot read properties of undefined (reading 'find') ReactJS TypeError:无法读取 ReactJS 中未定义的属性(读取“toString”) - TypeError: Cannot read properties of undefined (reading 'toString') in ReactJS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM