I understand the var city is not getting defined by debugging it. First time execution Second time execution Actual code :
var request = new XMLHttpRequest()
request.open('GET', 'https://restcountries.eu/rest/v2/all', true)
var city;
request.onload = function() {
var data = JSON.parse(this.response);
var findName = data.find((item) => {
return item.name.toUpperCase() == 'INDIA';
})
city = findName.capital;
}
request.send();
var request = new XMLHttpRequest();
link = "https://api.openweathermap.org/data/2.5/weather?q=" + city + "&units=metric&apikey=" + "bb0a5a97e99ae93b148d219df2f4e97f";
request.open('GET', link, true);
request.onload = function() {
var obj = JSON.parse(this.response);
if (request.status >= 200 && request.status < 400) {
var temp = obj.main.temp;
console.log(temp)
} else {
console.log("The city doesn't exist! Kindly check");
}
};
request.send();
can you please change the code so , it works everytime