0

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,
   }
}
Ezra Siton
  • 6,887
  • 2
  • 25
  • 37
  • Your `getExchangeRates` does not `return` anything – Bergi Oct 30 '20 at 23:05
  • 2
    I don't see `getExchangeRate` anywhere, do you mean `getExchangeRates`? Can you post the exact error message? – Barmar Oct 30 '20 at 23:06
  • 1
    It's not saying that the function name is undefined, it's saying that the value returned by it is undefined. Since it doesn't have a `return` statement, it returns `undefined` by default. – Barmar Oct 30 '20 at 23:08
  • Use `console.log` looks like you don't get inside your JSON data correctly. Inside `getExchangeRates` ==> `ex.x` - No way to return object data like this (`x` is a var). `return ex.x;` ==> change to `return ex[x];` – Ezra Siton Oct 30 '20 at 23:16
  • reserByra-model.js:50 ReferenceError: getExchangeRates is not defined at reserByra-model.js:34 –  Oct 30 '20 at 23:18
  • @Bergi , if you check properly inside the function getExchangeRates, it returns ex.x. –  Oct 30 '20 at 23:23
  • @Ezra Siton, i've changed the return to ex[x], but still get the same error. –  Oct 30 '20 at 23:27
  • @DanAroka If you check properly, that is a `return` statement of the `then` callback function, not of `getExchangeRates`. – Bergi Oct 30 '20 at 23:30
  • @Bergi, where then can i place the return? –  Oct 30 '20 at 23:40
  • Also, your issue related to the return value from a promise (Clean up your code first and try to return correctly simple "`Hello world`" message). Maybe helpful: https://javascript.info/promise-chaining ////////// https://stackoverflow.com/questions/34094806/return-from-a-promise-then – Ezra Siton Oct 30 '20 at 23:42
  • 2
    You need to place another `return` in front of the `fetch(…` so that you are returning your promise chain. Notice that you cannot synchronously return the conversion rate, you'll return a promise and will have to wait for it in your `getCityInformation` function – Bergi Oct 30 '20 at 23:42
  • @Bergi, i had a seperate asyn-wait fetch function that returns a promise, then calling it in the getExchangeRates(). async exchangeRates() { let promise = await fetch('https://v6.exchangerate-api.com/v6/547f5391104d30d830f55442/latest/SEK') return promise; } getExchangeRates = function(x) { exchangeRates() .then((res) => res.json()) .then((data) => { let ex= data.conversion_rates; console.log(ex); return ex[x]; //Get the exchange rate for the respective country –  Oct 31 '20 at 00:00
  • @Barmar, yes it's getEchangeRates(). Error message is : reserByra-model.js:55 ReferenceError: getExchangeRates is not defined –  Oct 31 '20 at 00:39

0 Answers0