I'm trying to make a weather app in order to learn some ReactJS.
I called the OpenWeatherMap API using this fetch in a componentDidMount method inside of my WeatherCard component.
constructor() {
super()
this.state = {
weatherData: {}
}
}
componentDidMount() {
fetch('//api.openweathermap.org/data/2.5/weather?q=Calgary,ca&APPID=XXX')
.then(response => response.json())
.then(data => {
this.setState({
weatherData: data
})
})
}
Here's a sample JSON output of the above call (given a valid API key):
I get this error message whenever I want to access a weather property:
TypeError: Cannot read property '0' of undefined
... and I access it like this:
this.state.weatherData.weather[0].main
Here is also my render method if it helps with the problem:
render() {
return (
<div>
{this.state.weatherData.weather[0].main}
</div>
)
}
Does anyone know what might be the problem I'm running into? Thanks a lot!
