Meteor.connection._lastSessionId is undefined in onCreated


#1

So I have this code guys

Template.mainLayout.onCreated(function () { //HERE
  console.log("mainLayout created");
  var context = FlowRouter.current();
  // use context to access the URL state
  console.log(context);
  var visitedOne = context.path;

  //getting the connID

  var clientIp = headers.getClientIP(); // no need for this anymore
  var clientConnId = Meteor.connection._lastSessionId; // HERE
  console.log(clientIp);
  console.log(clientConnId); //HERE
  // console.log(Meteor.connection._lastSessionId);



  Meteor.call("updateHistory", {clientIp,clientConnId,visitedOne}, function(error, result){
   if(error){
     console.log("error", error);
 });
   if(result){

   }
  });//Meteor.call
});

My problems are marked by the comments //HERE

Meteor.connection._lastSessionId returns undefined at onCreated event. However if I try to get on click event it works just fine. Why is this caused, what’s a workaround for this?


#2

I suspect it’s a timing thing. The session Id hasn’t yet made the trip back to the client.


#3

Yeah, that is what I am thinking as well. Do you think using timers here is a good solution, or you can suggest a better one?


#4

I don’t like using timers to work around issues. I would need to do some experimenting, but the source seems to suggest that if you were to use the Meteor.status() reactive source within an autorun, then when this becomes connected, the DDP message (which contains the session Id) will have been parsed and so _lastSessionId will also be available.