0

This may well be a result of the fact I am new to node and javascript in general. Me and a friend have been working on a steam App to update a CS:GO rank for a given user. All code works - e.g. checking if user is still in friend list etc. but we seem to have an issue with actually using a foreach on an SQL query which grabs all users in a table and wishes to update information about them.

CSGOCli.playerProfileRequest(CSGOCli.ToAccountID(value.steam64ID));
                            CSGOCli.on("playerProfile", function(profile) {

                                console.log('UPDATE counterStrikeGlobalOffensive SET clientRank = "'+ profile.account_profiles[0].ranking.rank_id +'",steamNick = "'+ name +'", wins = "'+ profile.account_profiles[0].ranking.wins +'", isVACBanned = "'+ profile.account_profiles[0].vac_banned +'" WHERE steam64ID = "'+ value.steam64ID +'"');

                                sqlll = 'UPDATE counterStrikeGlobalOffensive SET clientRank = "'+ profile.account_profiles[0].ranking.rank_id +'",steamNick = "'+ name +'", wins = "'+ profile.account_profiles[0].ranking.wins +'", isVACBanned = "'+ profile.account_profiles[0].vac_banned +'" WHERE steam64ID = "'+ value.steam64ID +'"';

                                con.query(sqlll, function (err, result) {
                                    if (err) throw err;
                                    console.log("1 record updated - csgo info.");
                                });

                                gameId = null;
                            });

So whats the problem? As you can see we do use a foreach rows. statement - the SQL itself is a simple SELECT * -

con.query("SELECT * FROM counterStrikeGlobalOffensive", function (err, rows) {

inside of which the above code is placed.

I come from a heavy php background so this may well be a conceptual problem - I'm thinking I need to reset the variables or something like that? Either way, its the values of

profile.account_profiles[0].ranking.rank_id
profile.account_profiles[0].ranking.wins
profile.account_profiles[0].vac_banned

that seem to get assigned once and once only.

Is there something I'm clearly doing wrong?

It is rare I ask a question, so I do apologise if something is unclear - just not sure what exact information to give on something which seems like a rather trivial thing to mess up on.

Thanks

Pantoflarz
  • 76
  • 7
  • 1
    Possible duplicate of [JavaScript closure inside loops – simple practical example](https://stackoverflow.com/questions/750486/javascript-closure-inside-loops-simple-practical-example) – abhishekkannojia Aug 02 '17 at 12:42
  • It's not exactly a duplicate, I've seen that post but I am struggling to apply it to my situation - I'm new to the language and like I mentioned, conceptually it is still something I am trying to get my head around. – Pantoflarz Aug 02 '17 at 12:43
  • Try wrapping your function call `s.getPlayerSummaries()` in a Immediately Invoked Function Expression. See this [answer](https://stackoverflow.com/a/19324832/2386736) for details – abhishekkannojia Aug 02 '17 at 12:47
  • is my use of a foreach a good idea here or should I stick to a for loop with a counter? Just wondering if this has any effect. – Pantoflarz Aug 02 '17 at 12:50
  • No, it does not matters in this case, you can get the value of index in both the cases. – abhishekkannojia Aug 02 '17 at 12:53
  • The steam64ID changes with every iteration, but nothing else does. – Pantoflarz Aug 02 '17 at 13:02
  • Probably because you've declared variables `myData`, `gameId`, `name` to be global (declaration without the keyword `var`) making the values same for all the callback functions. Try declaring them with `var` – abhishekkannojia Aug 02 '17 at 13:10
  • Changed the question with an Edit using vars now instead of global declaration. Name and steam64ID change, but nothing else does. – Pantoflarz Aug 02 '17 at 13:20

0 Answers0