简体   繁体   English

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

[英][Unhandled promise rejection: TypeError: undefined is not an object (evaluating 'this.state.result.map')]

[Unhandled promise rejection: TypeError: undefined is not an object (evaluating 'this.state.result.map')]. [未处理的 promise 拒绝:TypeError:未定义不是 object(评估 'this.state.result.map')]。 I am getting this error whenever i am trying to fetch the data from the api.每当我尝试从 api 获取数据时,都会出现此错误。 And also below in code i have used val.Global where Global is the title under which there is all the data.在下面的代码中,我使用了 val.Global,其中 Global 是所有数据的标题。 Can anyone tell what's the problem in my code?谁能告诉我的代码有什么问题?

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'
    }
  });

Your API call returns this:您的 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": {}
            },
...

And in your component, you're trying to retrieve the "covid" field, which doesn't exist... You need to save your API data like this:在您的组件中,您正在尝试检索不存在的“covid”字段...您需要像这样保存 API 数据:

this.setState({
    isloading: false,
    result: responseJson
})

And change your render this way:并以这种方式更改您的渲染:

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>
   );
}

The error is caught up because you are trying to go through a table that doesn't exist.由于您试图通过一个不存在的表来 go ,因此该错误已被捕获。

I was working on a similar project.我正在做一个类似的项目。 Guess this should suffice猜猜这应该足够了

const covid = result.Countries.map((country, key) => {
            return (<View key={key} style={styles.item}><Text>{country.TotalRecovered}</Text></View>);
        })

    return (
                <View style={styles.container}>{name}</View>
            )

Guess this should suffice!猜猜这应该足够了!

暂无
暂无

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

相关问题 未处理的 Promise 拒绝:类型错误:未定义不是 object(正在评估“data.date”) - Unhandled Promise Rejection: TypeError: undefined is not an object (evaluating 'data.date') [未处理的承诺拒绝:类型错误:未定义不是对象(评估&#39;_ref.about&#39;)] - [Unhandled promise rejection: TypeError: undefined is not an object (evaluating '_ref.about')] [未处理的承诺拒绝:类型错误:未定义不是对象(评估“response.data”)] - [Unhandled promise rejection: TypeError: undefined is not an object (evaluating 'response.data')] 未处理的承诺拒绝:TypeError:未定义不是一个函数,当在react native中为setState映射时会发生&#39;departureloc.map&#39;评估 - Unhandled promise rejection: TypeError: undefined is not a function evaluating 'departureloc.map' occurs when mapping for setState in react native React Native:未处理的promise promise:TypeError:undefined不是对象(评估&#39;response.json&#39;) - React Native: Unhandled promise rejection: TypeError: undefined is not an object (evaluating 'response.json') 未处理的 promise 拒绝:TypeError: undefined is not an object(评估 '_context.t0.data.error')在 expo - Unhandled promise rejection: TypeError: undefined is not an object (evaluating '_context.t0.data.error') in expo 警告可能未处理的 Promise 拒绝 (id: 21): TypeError: undefined is not an object (evaluating 'res.json') - WARN Possible Unhandled Promise Rejection (id: 21): TypeError: undefined is not an object (evaluating 'res.json') Expo:未处理的 promise 拒绝:TypeError:未定义不是 object(评估“props.navigation”)] - Expo: Unhandled promise rejection: TypeError: undefined is not an object (evaluating 'props.navigation')] 谷歌地图地理编码 API 错误 [未处理的 promise 拒绝:TypeError:undefined 不是 object(评估 'data.results[0].geometry')] - Google maps Geocoding API error [Unhandled promise rejection: TypeError: undefined is not an object (evaluating 'data.results[0].geometry')] 可能未处理的承诺拒绝(Id:0):TypeError:undefined不是对象(评估&#39;err.response.data&#39;) - Possible Unhandled Promise Rejection (Id: 0): TypeError: undefined is not an object (evaluating 'err.response.data')
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM