首页 > 解决方案 > 无法读取属性“未定义的统计信息”

问题描述

在此处输入图像描述我试图用 axios + node.js 进行 api 调用,我在控制台中获取数据但是当我尝试检索嵌套值时我得到错误无法读取未定义的属性“统计”。在控制台中,当我将鼠标悬停在它上面时,我得到 data.data.[0].statistics.population_density.value 但在我的代码中它不起作用。有人可以解释 imi 做错了什么吗?谢谢

 [
   data:
      {
        data: Array(1) {

          0: {

  {
    "statistics": {
      "population_density": {
        "source_name": "NASA Socioeconomic Data and Applications Center (SEDAC) – Hosted by CIESIN at Columbia University",
        "value": 13543,
        "description": "The number of inhabitants per square kilometer around this point."
      }
    },
    "location": {
      "latitude": 37.769456,
      "longitude": -122.429128
    }
  }
]


handleSelect = address => {
    this.setState({
        address,
    });

    console.log(this.state.address);

    geocodeByAddress(address)
        .then(res => getLatLng(res[0]))
        .then(({
            lat,
            lng
        }) => {
            this.setState({
                latitude: lat,
                longitude: lng,
                isGeocoding: false,
            });

            this.setState({
                isLoaded: true
            });
        })
        .catch(error => {
            this.setState({
                isGeocoding: false
            });
            console.log('error', error); // eslint-disable-line no-console
        });

    console.log(this.state.latitude);
    console.log(this.state.longitude);

    var param = {
        lat: this.state.latitude,
        long: this.state.longitude,
        temp: 1,
    };
    axios
        .post(`http://localhost:5000/search-data`, {
            param,
        })
        .then(data => {
            console.log(data);
            this.setState({
                value: data.data[0].statistics.population_density.value,
            });

        });

};

标签: reactjs

解决方案


你需要data.data.data[0]

.then(data => {
        console.log(data);
        this.setState({
            value: data.data.data[0].statistics.population_density.value,
        });

    });

的输出console.log(data)表明该对象(存储在名为的变量中data)具有一个名为的属性,该data属性又具有一个名为 的属性data


推荐阅读