React .map()不是函数错误

时间:2017-03-24 01:10:16

标签: javascript reactjs ecmascript-6 redux axios

import React, {Component} from 'react';
import './App.css';
import axios from 'axios';

export default class App extends Component {

    constructor() {
        super();
        this.state = {
            weather: []
        };
    }
componentDidMount (){

  this.search();
}
    search = () => {

        axios.get("http://api.openweathermap.org/data/2.5/weather?q=Houston&APPID=f2327cca8f3d665f4c9f73b615b294ed")
        .then(res => {

            this.setState({weather: res.data});

        }).catch(error => {
            console.log('Error from fetching data', error);
        })
    }

    render() {
    console.log(this.state.weather);
    const weathermap = this.state.weather.map( (maps) =>{
      {maps.wind.speed}
});

        return (
            <div className="container">
                <div className="row">
                    <div className="col-md-4 col-md-offset-4">
                        <div className="weather">
                            <div className="current">
                                <div className="info">
                                    <div>&nbsp;</div>
                                    <div className="city">
                                        <small>
                                            <small>CITY:</small>
                                        </small>
                                        {this.state.weather.name}</div>
                                    <div className="temp">67&deg;
                                        <small>F</small>
                                    </div>
                                    <div className="wind">
                                        <small>
                                            <small>WIND:{weathermap}</small>
                                        </small>

                                        </div>
                                    <div>&nbsp;</div>
                                </div>
                                <div className="icon">
                                    <span className="wi-day-sunny"></span>
                                </div>
                            </div>
                            <div className="future">
                                <div className="day">
                                    <h3>Mon</h3>
                                    <p>
                                        <span className="wi-day-cloudy"></span>
                                    </p>
                                </div>
                                <div className="day">
                                    <h3>Tue</h3>
                                    <p>
                                        <span className="wi-showers"></span>
                                    </p>
                                </div>
                                <div className="day">
                                    <h3>Wed</h3>
                                    <p>
                                        <span className="wi-rain"></span>
                                    </p>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

        );
    }
}

我收到“this.state.weather.map不是函数”错误。我从天气api获取数据。我从api显示的名字显示确定。 api称自己是好的也是成功。 这里api在控制台enter image description here

中的样子

这是代码

3 个答案:

答案 0 :(得分:5)

您正在通过说set.seed(123) df <- data.frame( group=rep(c('group1','group2','group3','group4'), 25), subgroup=rep(c('subgroup1','subgroup2','subgroup3','subgroup4'), 25), v1=rnorm(100), v2=rnorm(100) ) cors_boot <- df %>% group_by(group,subgroup) %>% bootstrap(10, by_group=TRUE) %>% group_by(group, subgroup) %>% #add in this line to make your code work do(tidy(cor.test(.$v1,.$v2))) cors_boot 来实例化该应用。但是,当您说this.state = { weather: []};时,您将this.setState({weather: res.data})覆盖为JavaScript this.state.weather而不是object,因此array不再可用。

只需.map

即可实现您的目标

答案 1 :(得分:2)

this.state.weather是一个javascript对象,所以.map不可用,你可以使用

Object.Keys(YourObject).map() 

答案 2 :(得分:1)

您应该先进行条件检查,然后再执行.map之类的

.map具有返回语法

const { weather } = this.state;
const weathermap = weather && weather.map(maps => {
      return <span>{maps.wind && maps.wind.speed}</span>
});

.map没有返回语法

const { weather } = this.state;
const weathermap = weather && weather.map(maps => (
     <span>{maps.wind && maps.wind.speed}</span>
));