简体   繁体   English

无法读取属性“未定义的统计信息”

[英]cannot read property “statistics of undefined”

在此处输入图片说明 im trying to make api call with axios + node.js , im getting data in console however when i try to retrieve the nested values im getting error Cannot read property 'statistics' of undefined. 我试图用axios + node.js进行api调用,我在控制台中获取数据,但是当我尝试检索嵌套值时,我得到错误无法读取未定义的属性'statistics'。 in console when i hover over it i get data.data.[0].statistics.population_density.value But in my code it doesnt work. 在控制台中,当我将鼠标悬停在它上面时,我得到data.data。[0] .statistics.population_density.value但在我的代码中它不起作用。 can someone explain what imi doing wrong ? 有人可以解释imi做错了什么吗? thanks 谢谢

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

        });

};

You need data.data.data[0] : 您需要data.data.data[0]

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

    });

The output of console.log(data) shows that this object - which is stored in a variable named data - has a property that is called data which in turn again has a property called data . console.log(data)的输出显示,此对象(存储在名为data的变量中)具有一个称为data的属性,而该属性又又具有一个名为data的属性。

I would suggest to rename the data parameter to response because that's what axios is actually giving you: 我建议将data参数重命名为response因为那是axios实际为您提供的:

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

Then the error should be obvious. 然后错误应该很明显。 A response does not consist only of data. response不仅包含数据。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM