javascript - [未处理的 promise 拒绝 : TypeError: undefined is not an object (evaluating 'this.state.result.map' )]

标签 javascript android reactjs api react-native

[未处理的 promise 拒绝:类型错误:未定义不是对象(评估'this.state.result.map')]。
每当我尝试从 api 获取数据时,我都会收到此错误。在下面的代码中,我使用了 val.Global,其中 Global 是所有数据的标题。谁能告诉我的代码有什么问题?

import React from 'react';
import {View, Text, ActivityIndicator, StyleSheet, FlatList} from 'react-native';



export class mainScreen extends React.Component{
  
    constructor(props){
        super(props);
        this.state = {
            isloading: true,
            result: []
        }
    }
    componentDidMount(){
     return fetch('https://api.covid19api.com/summary')
      .then((response) => response.json())
      .then((responseJson) => {
          this.setState({
              isloading: false,
              result: responseJson.covid
          })
      })
    }
   
    render(){
        if(this.state.isloading){
            return (
                <View style={styles.container}>
                <ActivityIndicator/>
                </View>
            )
        }else{
        let covid = this.state.result.map((val, key) => {
        return <View key={key} style={styles.item}><Text>{val.Global}</Text></View>
        });
       return(
        <View style={styles.container}>
        {covid}
       </View>
        );
    }
  }
}
const styles = StyleSheet.create({
    container: {
      flex: 1,
      backgroundColor: '#fff',
      alignItems: 'center',
      justifyContent: 'center',
    },
    item: {
        flex: 1,
        alignSelf: 'stretch',
        margin: 10,
        alignItems: 'center',
        justifyContent: 'center'
    }
  });

最佳答案

您的 API 调用返回:

{
    "Global": {
        "NewConfirmed": 214569,
        "TotalConfirmed": 14506757,
        "NewDeaths": 4029,
        "TotalDeaths": 606157,
        "NewRecovered": 104134,
        "TotalRecovered": 8149310
    },
    "Countries": [
        {
                "Country": "Afghanistan",
                "CountryCode": "AF",
                "Slug": "afghanistan",
                "NewConfirmed": 174,
                "TotalConfirmed": 35475,
                "NewDeaths": 17,
                "TotalDeaths": 1181,
                "NewRecovered": 361,
                "TotalRecovered": 23634,
                "Date": "2020-07-20T13:49:18Z",
                "Premium": {}
            },
...
在您的组件中,您试图检索不存在的“covid”字段......您需要像这样保存您的 API 数据:
this.setState({
    isloading: false,
    result: responseJson
})
并以这种方式更改渲染:
render() {
    const { loading, result } = this.state; 

    if (loading) {
        return (
            <View style={styles.container}>
                <ActivityIndicator/>
            </View>
        );
    }

    //If you want to show total recovered for global
    return (<View key={key} style={styles.item}><Text>{val.Global.TotalRecovered}</Text></View>);

   //If you want to show global recovered for each country
   const covid = result.Countries.map(country => (<View key={key} style={styles.item}><Text>{country.TotalRecovered}</Text></View>));

   return (
        <View style={styles.container}>
            {covid}
        </View>
   );
}
错误被捕获,因为您正试图通过一个不存在的表。

关于javascript - [未处理的 promise 拒绝 : TypeError: undefined is not an object (evaluating 'this.state.result.map' )],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62995677/

相关文章:

html - 如何在不使用 JavaScript cookie 的情况下在客户端计算机上存储信息

reactjs - 如何在 Cypress 中测试 React Material UI Drawer 组件属性值

javascript - Browserify 错误 : Parsing file, 意外的 token

javascript - 本地图屏幕打开时,如何使 map View 以用户当前位置为中心? React Native 博览会

android - 如何在android中停止intentservice?

Javascript 功能在主页上运行,但在第二页上停止

javascript - Jquery click 函数只被调用一次

javascript - document.getElementById 类适用于整个数组而不是每个元素

android - 用TextView定位复合Drawable?

android - 我想在 z 轴上旋转 imageview