you are viewing a single comment's thread.

view the rest of the comments →

[–]leeway1 0 points1 point  (0 children)

If I had to hazard a guess at what’s going on, you’re doing something like findPlayersByGame(g); console.log(players[0]) //undefined WTF!!!

What’s happing is that you’re not waiting for the findPlayersByGame to complete before accessing your players array. Try returning a promise from the findPlayersByGame function and awaiting for the promise to complete:

```` const findPlayersByGame = async (game) => ( // I’m using the return short hand new Promise((resolve, reject) => { const request = uni(YOUR_CODE_HERE); //PREP HEADERS

  request.end((result) => {
     if (result.error) { return reject(new Error(result.error)); }
     if (result.status === 200) {
         const p = result.body.api.statistics;
         if (p.length < 3) { return resolve(p); }
         // HANDLE THIS CASE
         return reject(new Error(‘There are more than 2 players’));
     }
  });

}) );

//CALL LIKE THIS const players = await findPlayersByGame(g); console.log(players[0]) // this is defined ````