【发布时间】:2019-06-20 07:45:34
【问题描述】:
我正在尝试对从 axios.get 方法(例如“console.log(data.results)”获得的 JSON 数据中包含的数组使用 map 方法。当我尝试访问其中的数据时,会出现以下错误:
TypeError: Cannot read property 'result' of undefined.
但是,当我输入“console.log(data)”时,它会毫无问题地记录 json 数据。这是我写的代码和数据对象中的json数据。
componentDidMount() {
axios.get('https://api.nytimes.com/svc/topstories/v2/science.json?api-key=privatekey')
.then(result=>
this.setState({jsonData:result})
)
.catch(error=> console.log(error))
}
render() {
return(
<div id="cards-wrapper">
{console.log(this.state.jsonData.data)}
</div>
)
}
<!-- console error-->
{status: "OK", copyright: "Copyright (c) 2019 Company. All Rights Reserved.", section: "science", last_updated: "2019-01-26T15:57:14-05:00", num_results: 28, …}
copyright: "Copyright (c) 2019 Company. All Rights Reserved."
last_updated: "2019-01-26T15:57:14-05:00"
num_results: 28
results: (28) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
section: "science"
status: "OK"
__proto__: Object
如何访问数据中的“结果”数组?谢谢你的帮助!
【问题讨论】:
标签: javascript ajax reactjs axios