我目前正在开发一个网站,在这里可以检索团队信息:
export default class TeamInfo extends React.Component{
constructor(props) {
super(props);
this.state = {
isShow: true,
team: []
};
this.getTeam();
}
getTeam(){
const axios = require("axios");
const team_id = this.props.id;
axios.get(API+'/team/'+ team_id).then(res => {
this.setState({team : res.data})
console.log('inside teaminfo... ' + this.state.team.location.city);
})
}
render() {
return(
<div><h1>{this.state.team.location.city}</h1></div>
)}
}
这是团队 JSON 答案的结构:
{
"name": "Barcelona",
"shield": "shield.png",
"location": {
"city": "Barcelona",
"country": "SPAIN"
},
"score": 74626,
}
我正在尝试访问团队位置 this.state.team.location.city ,因为当我在控制台中登录时它显示正确,但Unhandled Rejection (TypeError): Cannot read property 'city' of undefined显示在网站中。
任何提示或帮助将不胜感激。
慕姐4208626
慕勒3428872
墨色风雨
相关分类