user1262878
user1262878

Reputation: 67

Meteor woun't save session value

Im trying to use Session.get() and Session.set() to check if a user has clicked a button. In the event this code is implemented to get check if the session is set.

"click .alt": function(event,template){

        if (Session.get("selected") === false){
            var clicked = event.currentTarget;
            clicked.className += " chosen";
            var data = clicked.dataset; 
            console.log(data);
            Session.set("selected", true);
        if (data.corr == true){
            console.log("Hi");
        }
        else{
            console.log("Not Hi");
        }
    }
    else{
        console.log("session was set to true");
    }
}

The consol.log(data) is always printed but "Hi" is newer printed even when data.corr=true. For some reason unknown, to me, this does not work. Hopefully someone can help me.

Update This wount work:

    if (Session.get("selected") == false){
        Session.set("selected", true);
        var clicked = event.currentTarget;

        if (clicked.dataset.corr == true){
            //Recording the time and calculating some points                
            time = new Date;
            time = time.getTime();
            time = 3000-Math.floor((time -Session.get("startedTime"))/10 );
            gameCol.insert({team:Session.get("teamnr"), points: time});
            console.log("Correct answer");
        }
        else{
            //0 points for wrong answer
            gameCol.insert({team:Session.get("teamnr"), points: 0});
            console.log("Wrong answer");
        }
    }
    else{
        console.log("You have answered");
    }

This works:

    if (Session.get("selected") == false){
        Session.set("selected", true);
        var clicked = event.currentTarget;

        if (clicked.dataset.corr == true){
            //Recording the time and calculating some points                
            time = new Date;
            time = time.getTime();
            time = 3000-Math.floor((time -Session.get("startedTime"))/10 );
            gameCol.insert({team:Session.get("teamnr"), points: time});
            console.log("Correct answer");
        }
        else{
            //0 points for wrong answer
            //gameCol.insert({team:Session.get("teamnr"), points: 0});
            console.log("Wrong answer");
        }
    }
    else{
        console.log("You have answered");
    }

The only difference is that the row for inserting a wrong answer in the db is remover. See the line //gameCol.insert({team:Session.get("teamnr"), points: 0});

Upadate 1.1 Changing the if/else to two if instead works

if (clicked.dataset.corr == true){
            //Recording the time and calculating some points                
            time = new Date;
            time = time.getTime();
            time = 3000-Math.floor((time -Session.get("startedTime"))/10 );
            gameCol.insert({team:Session.get("teamnr"), points: time});
            console.log("Correct answer");
        }
        if(clicked.dataset.corr == false){
            //0 points for wrong answer
            gameCol.insert({team:Session.get("teamnr"), points: 0});
            console.log("Wrong answer");
        }

Upvotes: 0

Views: 153

Answers (3)

user1262878
user1262878

Reputation: 67

I think i found out what the problem was and probably was it me mot understanding Meteor enough. I think that when a reactive data is used in a template a change in any bound template data triggers a recompute of all bound data in the template. The session data is set and used also in a function that is bound in the template. Therefor as soon as a recompute was triggered the session was set to false again.

As soon as I broke my templates in smaller pieces each consisting of a separate template this behavior ended.

Upvotes: 0

Akeel Nazir
Akeel Nazir

Reputation: 21

Try if (Session.equals('selected', false)) instead of if (Session.get('selected') === false. It should work.

Upvotes: 0

Kristoffer K
Kristoffer K

Reputation: 2053

It does work. I added your code (removed some stuff that I don't know what it was for) to a vanilla meteor project and it works just fine.

I created a project using mrt create, and this code sets the session from true to false on each click on the hello-template.

if (Meteor.isClient) {

  Meteor.startup(function() {
    Session.set("selected", false);
  });

  Template.hello.greeting = function () {
    return "Welcome to so-q.";
  };

  Template.hello.events({
    "click": function(event,template){

      if (Session.get("selected") === false){
        console.log("session was set to false");
        Session.set("selected", true);
      }
      else{
        console.log("session was set to true");
        Session.set("selected", false);
      }
    }
  });

}

if (Meteor.isServer) {
  Meteor.startup(function () {
// code to run on server at startup
});
}

Upvotes: 0

Related Questions