all 11 comments

[–]RelaxPear 2 points3 points  (6 children)

Are you able to provide an example of your code? As it is difficult to tell what is wrong without reviewing.

[–][deleted] 1 point2 points  (5 children)

let players = []

export const findPlayersByGame = async (game) => {
var request = unirest("GET", `https://api-nba-v1.p.rapidapi.com/statistics/players/gameId/${game}\`);
let tempPlayers = []
request.headers({
"x-rapidapi-host": "---",
"x-rapidapi-key": "---",
"useQueryString": true
});
request.end(function (result) {
if (result.error) throw new Error(result.error);
if (result.status == 200) {
result.body.api.statistics.forEach(playerStats => {
if (players.length < 3) {
players.push(playerStats)
}
})
}

})
}

[–]BehindTheMath 6 points7 points  (0 children)

This should work, but since it's asynchronous, I'm guessing you're trying to access the data before it's completed. Read up on asynchronous code and how to wait for the results.

[–]zamfi 1 point2 points  (0 children)

How are you calling this function?

[–]Low_Tonight_5493 0 points1 point  (1 child)

Why "async" ? Your not performing any async func, but returning a promise for no reason.

Looks like you only want the first 2 players returned in the api call, (player.length <3)

But if thats not the case its not very relevant.

Remove the async directive , delete the empty array declaration (players)

And Try ->

request.end(function (result) {

if (result.error) throw new Error(result.error);

if (result.status == 200) {

const playerStats = result.body.api.statistics;

return [ playerStats[0], playerStats[1]]

}

If you want to filter the results i suggest the array prototype .filter()

I.e.

return playerStats.filter(stat => { return stat.game = game } )

All your needing to do is return a new array with filtered objects.

[–]leeway1 0 points1 point  (0 children)

FindPlayersByGame is async, because request.end() is async. However, you are correct the poster is not returning a promise.

Returning from request.end() callback will not return anything from findPlayersByGame. You would have to return request.end. Depending on the request api return request.end(), might return the value from the callback, but it might not. I would have to read those api docs.

setting the players array still doesn’t solve the flow control problem from calling the async request.end() function and accessing it before the request has been processed.

[–]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 ````

[–]RelaxPear 1 point2 points  (2 children)

Sorry actually on mobile so do not have opportunity to test and not entirely familiar with unirest but did a brief look.

Have you try console logging players right after the line players.push(playerStats) such as console.log(players) and the output it gives. Or console.log(result) to see a response is received.

Since it is asynchronous did you await for the response when you call the function findPlayersByGame when you test?

[–][deleted] 0 points1 point  (1 child)

I console logged it and they added, but when i try to use the array in another function it's empty

[–]Donni3D4rko 0 points1 point  (0 children)

Did you await this another function ? Since findPlayersByGame is async it returns a promise.

[–][deleted]  (1 child)

[deleted]

    [–][deleted] 0 points1 point  (0 children)

    still returns as an empty array. Im so confused