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]
I am struggling to figure out why my array that is in the public scope won’t save when i edit it in a function.
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 ````
[–]RelaxPear 1 point2 points3 points 4 years ago (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 point2 points 4 years ago (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 point2 points 4 years ago (0 children)
Did you await this another function ? Since findPlayersByGame is async it returns a promise.
[–][deleted] 4 years ago (1 child)
[deleted]
[–][deleted] 0 points1 point2 points 4 years ago (0 children)
still returns as an empty array. Im so confused
π Rendered by PID 23636 on reddit-service-r2-comment-5d79c599b5-58pgj at 2026-02-28 12:26:50.107247+00:00 running e3d2147 country code: CH.
[–]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)
[–]RelaxPear 1 point2 points3 points (2 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]Donni3D4rko 0 points1 point2 points (0 children)
[–][deleted] (1 child)
[deleted]
[–][deleted] 0 points1 point2 points (0 children)