Jump to content
McKay Development

Cannot read property 'player_name' of undefined


T1MOXA

Recommended Posts

Hello. I have a small problem...

Sometimes the bot crashes with an error "Cannot read property 'player_name' of undefined"

 

Here is my code: 


bot.on('friendsList', function() {
for (var steamID in bot.myFriends) {
if(bot.myFriends[steamID] === 2) {
bot.getPersonas([steamID], function(getName) {
var friendName = getName[steamID].player_name;
//Some code
});
}
}
});

Where I erred ?

Edited by T1MOXA
Link to comment
Share on other sites

You're overwriting the value of steamID in your outer loop, which changes the context of the closure. This is confusing, I know. Just do this and it should work:

bot.on('friendsList', function() {
  Object.keys(bot.myFriends).forEach(function(steamID) {
    if(bot.myFriends[steamID] === 2) {
      bot.getPersonas([steamID], function(getName) {
        var friendName = getName[steamID].player_name;
	//Some code
      });
     }
   });
});
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...