【发布时间】:2018-07-01 22:12:31
【问题描述】:
我正在尝试使用来自 REST API 的 JSON 数据填充 matches:[]。我无法从 REST API 获取()数据。
在 main.js 中:
import React, { Component } from 'react';
class Main extends Component {
constructor(props){
super(props);
this.state = {
matches:[]
};
}
componentDidMount(){
fetch('api/matches')
.then(res => this.setState({matches}, () => console.log('Matches fetched...', matches)));
}
render() {
return (
<div>
<div class="row">
<div class="col-lg-4" styles="background-color:lavender;">{this.state.matches[0].season}</div>
<div class="col-lg-4" styles="background-color:lavenderblush;">{this.state.matches[5].season}</div>
<div class="col-lg-4" styles="background-color:lavender;">{this.state.matches[8].season}</div>
</div>
</div>
);
}
}
export default Main;
我稍后将 JSON 数据填充到引导程序中以在网格系统中显示它,但我收到错误,这些行显示以下行的 TypeError:
this.state.matches[0].season
this.state.matches[5].season
this.state.matches[8].season
【问题讨论】:
-
你用 XHR 检查了网络选项卡吗?这个 API 的响应是什么
-
@RIYAJKHAN imgur.com/a/H0Krf
标签: javascript json reactjs rest