0

I have a getData () {...} function, inside it with fetch () and .then an object is populated which is declared in the global scope with var. After describing the body of the function, I immediately run it: getData (); then I output the object filled inside the function to the console. The object is displayed as it should, all the data is there. BUT! as soon as I try to do some kind of manipulation with the object, for some reason it is always empty and its length = 0 . What could be the problem and how to fix it?

var requestOptions = {
    method: 'GET',
    redirect: 'follow'
};

var countriesData = {};

function getData() {

    fetch('https://api.covid19api.com/summary', requestOptions)
      .then(res => res.json())
      .then(res => {
        covidGlobalCases.innerHTML = res.Global.TotalConfirmed;
      });

    fetch('https://api.covid19api.com/summary', requestOptions)
      .then(res => res.json())
      .then(res => res.Countries.forEach((country) => {
        //console.log(country.Country + ': ' + country.TotalConfirmed);
        countriesData[country.Country] = country.TotalConfirmed;
      }));
      
}

getData();
console.log(countriesData);
console.log(countriesData.length);

var sortedCountriesData = Object.keys(countriesData).sort(function(a, b) {
    return countriesData[a] - countriesData[b];
});
console.log(sortedCountriesData);

I tried to rewrite it, but I don't understand how to correctly rewrite this line.

      .then(res => res.Countries.forEach((country) => {
        //console.log(country.Country + ': ' + country.TotalConfirmed);
        countriesData[country.Country] = country.TotalConfirmed;
      }));
async function getData() {

    const covidGlobalCases = document.querySelector('.general-info__global__cases');
    const tableCasesByCountry = document.querySelector('.table__cases__by__country');

    let res = await fetch('https://api.covid19api.com/summary', requestOptions);
    let data = await res.json();
    covidGlobalCases.innerHTML = await data.Global.TotalConfirmed;

    res = await fetch('https://api.covid19api.com/summary', requestOptions);
    data = await res.json();
    data = await data.Countries.forEach((country) => {
        countriesAllConfirmed[country.Country] = country.TotalConfirmed;
        tableCasesByCountry.insertAdjacentHTML('beforeend', `
          <div class="table__tr">
             <span class="county__cases">${countriesAllConfirmed[country.Country]}</span> <span class="county__name">${country.Country}</span>
          </div>
        `);     
    })

    return countriesAllConfirmed;

}
trek89
  • 1
  • 2
  • peace of code: https://jsfiddle.net/svazi89/fzcqnLhm/ console output: https://radikalno.ru/full/2020/12/20/3b3b44dc5e5959bdbfb1f70906fa22ad-full.png.html – trek89 Dec 20 '20 at 08:55
  • Post the actual code. – dpwr Dec 20 '20 at 08:55
  • 1
    Your `getData` function doesn't return anything. Await the promises completion and then return the data you want – dpwr Dec 20 '20 at 08:58
  • Please put the code in the question – Jaromanda X Dec 20 '20 at 09:16
  • @dpwrussell, I tried it, but I don't understand how to correctly remake 12 and 13 lines. Could you tell? https://jsfiddle.net/svazi89/fzcqnLhm/5/ – trek89 Dec 20 '20 at 09:45

0 Answers0