[英]React: Uncaught TypeError: this.state.data.map is not a function and
I did some digging on this already but haven't come up with a good solution yet. 我已经对此进行了一些挖掘,但是还没有提出一个好的解决方案。 A similar question was asked here but my difference is that I'm not trying to pass a
string
to the map()
method. 在这里提出了类似的问题,但是我的区别是我没有尝试将
string
传递给map()
方法。
Anyways, I'm just building a simple weather checked app using React, I'm using the API from Open Weather Map API 5 day forecast. 无论如何,我只是使用React构建一个简单的天气检查应用程序,我使用的是Open Weather Map API 5天天气预报中的API 。
The API works fine, I can log the data to my console using the below: 该API工作正常,我可以使用以下方法将数据记录到控制台:
componentDidMount () {
axios.get('http://api.openweathermap.org/data/2.5/forecast?q=London,uk&appid=APIKEY')
.then(response => {
console.log(response.data);
});
}
But, when I got to map it to my array of component, I get the error from above, saying that this.state.forecasts.map()
is not a function: 但是,当我将其映射到组件数组时,我从上面得到了错误,说
this.state.forecasts.map()
不是一个函数:
Weather.js Weather.js
class Weather extends Component {
state = {
forecasts: []
}
componentDidMount () {
axios.get('http://api.openweathermap.org/data/2.5/forecast?q=London,uk&appid=APIKEY')
.then(response => {
this.setState({forecasts: response.data});
});
}
render() {
const projections = this.state.forecasts.map(forecast => {
return (<Subweather
date={forecast.dt_txt}
//temp={day.temp}
key={forecast.id} /> );
});
return (
<div className={classes.WeatherDiv}>
{projections}
</div>
I'm a little confused as to what I'm missing here, any suggestions? 我对这里缺少的内容有些困惑,有什么建议吗?
replace 更换
this.setState({forecasts: response.data});
with 同
this.setState({forecasts: response.data.list});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.