简体   繁体   English

反应:未捕获的TypeError:this.state.data.map不是函数,并且

[英]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.

相关问题 未捕获的类型错误:this.state.data.map 不是函数 - Uncaught TypeError: this.state.data.map is not a function React:如何解决&#39;Uncaught TypeError:this.state.data.map不是一个函数&#39; - React : How to fix ' Uncaught TypeError: this.state.data.map is not a function' 反应类型错误 this.state.data.map 不是 function - React TypeError this.state.data.map is not a function API.js:54 未捕获(承诺中)类型错误:this.state.data.map 不是 ZC1C42150748E78 - API.js:54 Uncaught (in promise) TypeError: this.state.data.map is not a function Reac js 类型错误:this.state.data.map 不是 function - Reac js TypeError: this.state.data.map is not a function Map React 中的问题 - this.state.Data.map 不是 ZC1C425268E18785D1AB5047 - Map Issue in React - this.state.Data.map is not a function 在 React js 中出现此错误的原因是什么 - TypeError: this.state.data.map is not a function - what's the reason to get this error in React js - TypeError: this.state.data.map is not a function this.state.data.map 不是 function 继续显示,即使我有一个数组 - this.state.data.map is not a function continues to show even though I have an array React 获取数据的问题 Uncaught TypeError: products.map is not a function - React problem fetching data Uncaught TypeError: products.map is not a function 未捕获的TypeError:this.state.map不是函数 - Uncaught TypeError: this.state.map is not a function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM