reactjs - 无法读取属性“未定义的统计信息”
问题描述
我试图用 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,
});
});
};
解决方案
你需要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
。
推荐阅读
- reactjs - Reactjs、GraphQL 和 Redux - 如何自动处理博客的分页?
- centos7 - CentOS7 ccollab 与 perforce CL 更新问题
- smtp - Adaptive Server Enterprise 数据库中的 UTL_SMTP 实用程序
- angular - 在 Angular 6 应用程序中托管 React 组件
- docker - Docker:通过 CLI 使用 --password 是不安全的。使用 --password-stdin
- tensorflow - 6 阶多项式回归无结果
- android - 错误:x86 仿真当前需要硬件加速!(安卓工作室)
- c++ - 使用 try/catch 处理字符串流错误
- apache-spark - 在 Spark 中并行读取多个 CSV 会减慢速度
- git - ubuntu 上的 git difftool 仅在终端上返回差异,而不是使用 meld