import React, {Component} from 'react';
import './App.css';
import axios from 'axios';
export default class App extends Component {
constructor() {
super();
this.state = {
weather: []
};
}
ponentDidMount (){
this.search();
}
search = () => {
axios.get(".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> </div>
<div className="city">
<small>
<small>CITY:</small>
</small>
{this.state.weather.name}</div>
<div className="temp">67°
<small>F</small>
</div>
<div className="wind">
<small>
<small>WIND:{weathermap}</small>
</small>
</div>
<div> </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>
);
}
}
import React, {Component} from 'react';
import './App.css';
import axios from 'axios';
export default class App extends Component {
constructor() {
super();
this.state = {
weather: []
};
}
ponentDidMount (){
this.search();
}
search = () => {
axios.get("http://api.openweathermap/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> </div>
<div className="city">
<small>
<small>CITY:</small>
</small>
{this.state.weather.name}</div>
<div className="temp">67°
<small>F</small>
</div>
<div className="wind">
<small>
<small>WIND:{weathermap}</small>
</small>
</div>
<div> </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>
);
}
}
I am getting a "this.state.weather.map is not a function" error. I am fetching data from weather api. I got the name from api displayed ok. api call it self is ok is success too. here how the api looks like in console
here is the code
Share Improve this question asked Mar 24, 2017 at 1:10 jsPlayerjsPlayer 1,2457 gold badges34 silver badges56 bronze badges 3-
If
this.state.weather
is a JS object, then it won't have the method.map
to it. However, if it's aImmutable.Map
, then yes it would have it. – Suthan Bala Commented Mar 24, 2017 at 1:13 -
this.setState({ weather: this.state.weather.concat([{ ...res.data }]) })
– Rei Dien Commented Mar 24, 2017 at 1:36 - would I display the date the same way I use setState with concat ? like this const weathermap = this.state.weather.map( (maps) =>{ {maps.wind.speed} – jsPlayer Commented Mar 24, 2017 at 2:38
3 Answers
Reset to default 6You are instantiating the app by saying this.state = { weather: []};
. However, When you say this.setState({weather: res.data})
, you are overriding the this.state.weather
to a JavaScript object
rather than array
, thus the .map
is not available anymore.
You can achieve what you're trying to do by simply const weathermap = this.state.weather.wind.speed
this.state.weather is a javascript object so .map is not available you can use
Object.Keys(YourObject).map()
You should do conditional check and then do .map like
.map with return syntax
const { weather } = this.state;
const weathermap = weather && weather.map(maps => {
return <span>{maps.wind && maps.wind.speed}</span>
});
.map without return syntax
const { weather } = this.state;
const weathermap = weather && weather.map(maps => (
<span>{maps.wind && maps.wind.speed}</span>
));