0

I'm using npm package steam-user

I'm stuck on getting steam name from steam id, I tried this:

  var name;
  client.getPersonas([sid], function(personas) {
    var persona = personas[sid];
    name = persona ? persona.player_name : ("[" + sid + "]");
  });

  console.log(config.console_userMessageReceived + "(" + name + " | " + sid + "): " + message);

However when the variable name is printed in console, its printed as undefined. Any help how to solve this, and print the actual steam name?

t.niese
  • 32,069
  • 7
  • 56
  • 86
Aeossa
  • 106
  • 9
  • what exactly is being used here? your question is unclear – mehulmpt Nov 24 '18 at 15:22
  • 4
    Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – jonrsharpe Nov 24 '18 at 15:22

1 Answers1

0

Seems like you have asynchronous call. Try this:

  var name;
  client.getPersonas([sid], function(personas) {
    var persona = personas[sid];
    name = persona ? persona.player_name : ("[" + sid + "]");
    console.log(config.console_userMessageReceived + "(" + name + " | " + sid + "): " + message);
  });
niry
  • 2,775
  • 19
  • 27
  • That kind of question has been answered with much more details multiple times. So it is better to mark it as duplicate than giving an incomplete answer. Besides that why do you still define `var name` outside of the callback if it can only be use within the callback then `name` should only be defined with that callback. – t.niese Nov 24 '18 at 16:02
  • @t.niese why don't you link the other answers or mark as duplicate? in the meanwhile, I solved someone's problem, while you were complaining and downvoting. – niry Nov 24 '18 at 16:29
  • @t.niese I agree that `var name` is misplaced. That wasn't the focus of the answer. – niry Nov 24 '18 at 16:36
  • @niry I didn't downvote your answer, how do you get to that assumption? A question having a large amount of detailed answers about that topic was already linked in the comments and voted for as duplicate, before you created your answer. And if you wouldn't have created an answer then I would have added that information you gave as answer, but as a comment. – t.niese Nov 24 '18 at 16:53
  • @t.niese, I'm happy to learn it wasn't you. I did upvoted the comment about dup. I also read the linked question/answers and I had a feeling it will be confusing and tl;dr, especially because the question and most answers discuses and uses as examples ajax / jquery. I'm not 100% sure it is a dup. – niry Nov 25 '18 at 14:38