0

i have created an object and i am passing an array and different objects to that array , but i cannot access them , it always gives , 0 is undefined or undefined when i want to access element at specific index , also even though there are more then 150 objects inside this array it still gives this message : length: 194 proto: Array(0)

so i think array is empty . result of console.log(data) on last line of code

    let passglobaldataArraytofun = (countryNames) => {
  let objforeachCountryStatistics = {};
  objforeachCountryStatistics.countries = [];
  for (let index = 0; index < countryNames.length; index++) {
    axios
      .get(`https://covid19.mathdro.id/api/countries/${countryNames[index]}`)
      .then((res) => {
        objforeachCountryStatistics.countries.push(res.data);
      })
      .catch((err) => {
        console.log(err);
      });
  }
  let data = objforeachCountryStatistics;
  console.log(data);
};

second method

async function passglobaldataArraytofun(countryNames){
  let objforeachCountryStatistics = {};
  objforeachCountryStatistics.countries = [];
  for (let index = 0; index < countryNames.length; index++) {
   let res= await axios.get(`https://covid19.mathdro.id/api/countries/${countryNames[index]}`)
     
objforeachCountryStatistics.countries.push(res.data);
     
     
  }
  console.log(objforeachCountryStatistics)
};
node Dev
  • 69
  • 1
  • 7
  • 3
    The above comment explains your problem but in a general way. Your code has 2 problems: 1) You are resetting your `globalArrayForstoringeachCountryandItsStatistics` every iteration of your loop and 2) Because `axios.get` is an asynchronous call, the result won't be available immediately after calling it. When you `console.log(data)` the promises will not have resolved yet and thus not have any data in them. – nbokmans Jul 07 '21 at 18:50
  • @nbokmans thanks,actually i am getting response but i want it to be in specific format thats why i am pushing it to an array and that array is a property in an object. – node Dev Jul 07 '21 at 18:50
  • @nbokmans yess u are right i took the code line globalArrayForstoringeachCountryandItsStatistics.countries=[] out of the loop – node Dev Jul 07 '21 at 18:51
  • you need to apply() the push(), otherwise they all go in index 0 – dandavis Jul 07 '21 at 18:52
  • @dandavis thanks , if u have time can u just make quick changes to the code? – node Dev Jul 07 '21 at 18:54
  • I don't want to leave you hanging without an answer and the question was closed. You are very close in your second function, just have to get the response json. Excuse the formatting: `passglobaldataArrayToFun = async countryNames => { const countryDataRequests = countryNames.map(name => axios.get('https://covid19.mathdro.id/api/countries/' + name).then(response => response.json())); const countryDataResponses = await Promise.all(countryDataRequests); console.log(countryDataResponses); return countryDataResponses; }` – nbokmans Jul 07 '21 at 19:21
  • @nbokmans dude thanks alot , i ran ur code but still i was getting the error res.json is not a function. – node Dev Jul 07 '21 at 19:32
  • Disregard that comment, `fetch` is not supported in `nodejs`, so instead replace just the `const countryDataRequests = ...` line with this: `const countryDataRequests = countryNames.map(name => axios.get('https://covid19.mathdro.id/api/countries/' + name).then(response => response.data));` – nbokmans Jul 07 '21 at 19:34
  • @nbokmans thanks for ur time but the last method that u suggested is exctly the same i use in my code , and i was getting all the responses , then i only wanted to store them in an array called countries which was property in an object called objforeachCountryStatistics , it all worked when i console.log it but i was just unable to access these objects. and even i had more than 150 objects in array its showd prototype Array(0) – node Dev Jul 07 '21 at 19:42
  • @nbokmans u can also view the final data that i got , in the image i provided. – node Dev Jul 07 '21 at 19:44

0 Answers0