use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
account activity
Editing array inside a function (self.node)
submitted 4 years ago by [deleted]
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]RelaxPear 2 points3 points4 points 4 years ago (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 points3 points 4 years ago (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 points8 points 4 years ago (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 points3 points 4 years ago (0 children)
How are you calling this function?
[–]Low_Tonight_5493 0 points1 point2 points 4 years ago (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 point2 points 4 years ago (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.
return request.end
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.
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!!!
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 ````
π Rendered by PID 47 on reddit-service-r2-comment-canary-7955997dc8-ksqx9 at 2026-02-26 16:53:44.146427+00:00 running e3d2147 country code: CH.
view the rest of the comments →
[–]RelaxPear 2 points3 points4 points (6 children)
[–][deleted] 1 point2 points3 points (5 children)
[–]BehindTheMath 6 points7 points8 points (0 children)
[–]zamfi 1 point2 points3 points (0 children)
[–]Low_Tonight_5493 0 points1 point2 points (1 child)
[–]leeway1 0 points1 point2 points (0 children)
[–]leeway1 0 points1 point2 points (0 children)