I am using metaweather.com API to build a Web Application. I want to show 6 cities on the home page; I guess I have to call the API 6 time and push the data in an array like allCitiesDetails
. How I have to do that? If there is a better way, please tell me. Here is my code :
state = {
city: {
cityname: this.props.value
},
woeid: '',
todayWeather: [],
weatherDetails: [],
allCitiesDetails: []
};
getCity = (cityName) => {
var self = this;
axios
.get('https://www.metaweather.com/api/location/search/?query=' + cityName)
.then(response => {
self.setState({
woeid: response.data[0].woeid
});
self.getWeather(response.data[0].woeid);
})
.catch(function(error) {
alert('No results were found. Try changing the keyword!');
});
}
getWeather = async (woeid) => {
const { data: weatherDetails } = await axios.get(
'https://www.metaweather.com/api/location/' + woeid
);
this.setState({
weatherDetails,
todayWeather: weatherDetails.consolidated_weather[0]
});
}