Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Having issue understanding JS Promise chaining

What am I missing, why is "jackpots" empty in the fixData function?

let jackpots = [];
let networks = [];
let games    = [];

async function getJackpots() {
  if(browser) {
    const res  = await fetch(`/api/jackpots/5`);
    const res2 = await fetch('/api/networks');
    const res3 = await fetch('/api/games');

    return {
        jackpots: res.ok && (await res.json()),
        networks: res2.ok && (await res2.json()),
        games: res3.ok && (await res3.json())
    }
  }  
}

async function fixData(jackpots, network, games) {
  
  jackpots.forEach((element) => {
    console.log(element)
  })

  return jackpots;
}

let getData = getJackpots().then((result) => {
    return result;
}).then((result) => {
    let fix = fixData(result?.jackpots, result?.networks, result?.games)
});

Error:

/src/lib/components/Jackpot/index.svelte:24
jackpots.forEach(element => {
^

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

TypeError: Cannot read properties of undefined (reading ‘forEach’)
at fixData (/src/lib/components/Jackpot/index.svelte:24:11)
at eval (/src/lib/components/Jackpot/index.svelte:53:13)

>Solution :

What dataType is jackpots? method forEach() only can be apply to arrays and this case probably jackpots isn’t array when your value is undefined

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading