I have created a function getExchangeRate()
to get the current exchange rate of a country. Then I'm calling the function inside another function to get the current exchange rate for a particular country. But the function getExchangeRate()
is considered undefined
.
getExchangeRates = function(x) {
fetch('https://v6.exchangerate-api.com/v6/547f5391104d30d830f55442/latest/SEK')
.then((res) => res.json())
.then((data) => {
const ex= data.conversion_rates;
return ex.x; //Get the exchange rate for the respective country
}).catch(function (error){
console.error('Something went wrong');
console.error(error);
});
}
getCityInformation = function(city) {
fetch('json/countries.json')
.then((res) => res.json())
.then((data) => {
for (let i=0; i<data.length; i++) {
if (data[i].city == city){
console.log([data[i].city, data[i].countryName, data[i].currency, data[i].exchangeRate]);
let currentExchange_rate = getExchangeRates(data[i].currency); // function is considered undefined
let output = '<h2>Details of the city</h2>'
output +='<ul><li>City Name: '+ data[i].city +
'</li><li>Country Name: '+ data[i].countryName +
'</li><li>Currency: '+ data[i].currency + // '</li><li>Currency: '+ currentExchange_rate +
'</li><li>Exchange Rate: '+ currentExchange_rate +
'</li>'
'</ul>';
document.getElementById('contents').innerHTML = output;
}
}
}).catch(function (error){
console.error('Something went wrong');
console.error(error);
});
}
Data return from:
fetch('https://v6.exchangerate-api.com/v6/547f5391104d30d830f55442/latest/SEK')
{
"result":"success",
"documentation":"https://www.exchangerate-api.com/docs",
"terms_of_use":"https://www.exchangerate-api.com/terms",
"time_last_update_unix":1604016243,
"time_last_update_utc":"Fri, 30 Oct 2020 00:04:03 +0000",
"time_next_update_unix":1604102763,
"time_next_update_utc":"Sat, 31 Oct 2020 00:06:03 +0000",
"base_code":"SEK",
"conversion_rates":{
"SEK":1,
"AED":0.4149,
"ARS":8.8436,
"AUD":0.1599,
}
}