【发布时间】:2019-11-06 03:15:54
【问题描述】:
我正在尝试从 GameSpot API 获取一些数据并显示它。我可以在控制台中看到这些数据,但在我的网页上看不到它。
import React, { Component } from 'react';
import axios from 'axios';
class Games extends Component {
constructor(){
super();
this.state={
games: [],
}
}
componentDidMount() {
axios.get(`http://www.gamespot.com/api/games/?api_key=[APIKEY]&format=json&filter=name:Cyberpunk 2077,limit:10`)
.then(res => {
this.setState({
games: res.data
})
})
.catch(error=>{
console.log(error);
})
}
render() {
const {games} = this.state;
console.log(games);
return (
<div>
<p>{games.name}</p>
</div>
);
}
}
我希望在我的网页上看到结果数据
编辑: console.log 给出:
【问题讨论】:
-
能否请您显示控制台数据?
-
好的,那么我认为你需要这样做:
{games.results[0].name}
-
它没有用。我收到此错误“TypeError: Cannot read property 'results' of undefined”