0

I'm trying to set a session variable inside a callback function as such:

getPlayerName() {

  Meteor.call("stocks.getPlayer", this.props.player.player, function(error, result){
  if(error){
    console.log(error.reason);
    return;
  }
    Session.set('playerName', result.Name);
  });

  console.log(Session.get('playerName'));

}

But the console on the client side returns undefined. I've also tried using reactive variables:

getPlayerName() {
  this.name = new ReactiveVar();
  Meteor.call("stocks.getPlayer", this.props.player.player, function(error, result){
  if(error){
    console.log(error.reason);
    return;
  }
    this.name.set(price);
  }.bind(this));

  console.log(this.name.get());

}

But this also returns undefined. How can I get this to work? Thank in advance.

Rew
  • 175
  • 7
  • 2
    You are calling console.log before the callback is completed and thus log an `undefined` because the variable has not been set yet. This is JS basics (callbacks). Please read on this topic first. – Jankapunkt Mar 11 '18 at 21:44
  • 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) – coagmano Mar 11 '18 at 21:59

0 Answers0