function - Meteor - Setting Session variables in Asynchronous Calls -


i have problem setting session variables inside of meteor.call function. appears meteor won't set session variables whatever ask outside of scope of function.

meteor.startup(function () {   // code run on server @ startup   // prompt name    var playername = prompt("please enter name:", "");   meteor.call('createplayer', playername, function(error, result) {     console.log("player_id: " + result);     session.set("myplayerid", result);     console.log("session_player_id: " + session.get("myplayerid"));   });    console.log("session_player_id2: " + session.get("myplayerid"));   session.set("gamestate", show_lobby); }); 

the console prints out:

player_id: correct id

session_player_id: correct id

session_player_id2: undefined

as can see, session variable no longer correct outside of scope of function. suggestions?

the call createplayer asynchronous, order of execution be:

  1. prompt user
  2. start call createplayer
  3. log myplayerid session variable (session_player_id2)
  4. finsh call createplayer: set myplayerid session variable in callback

because (4) execute after (3), undefined when trying log "session_player_id2". if that's thing going on (i.e. there isn't bug somewhere else in code), should able following in browser console:

console.log(session.get('myplayerid')); 

and, hopefully, correct result. answer original question: don't see wrong here - session variable still available outside of scope. appears misunderstanding of behavior of asynchronous functions.


Comments

Popular posts from this blog

My HTML document is not linking to my CSS stylesheet properly -

php array slice every 2th rule -

node.js - Sending sockets to client side, Error: Converting circular structure to JSON -