Skip to content

Instantly share code, notes, and snippets.

@tsega
Last active August 29, 2015 14:13
Show Gist options
  • Save tsega/1251316397d9b143bd4b to your computer and use it in GitHub Desktop.
Save tsega/1251316397d9b143bd4b to your computer and use it in GitHub Desktop.
Meteor Sessions
Router.onBeforeAction(function(){
var postSlug = this.params.slug;
var currentPost = Posts.findOne({slug: postSlug});
var anonymousSession = "anonymousViewers_"+currentPost._id;
var anonymousCount = Session.get(anonymousSession);
if(anonymousCount){
Session.set(anonymousSession, parseInt(anonymousCount) + 1);
}else{
Session.set(anonymousSession, 1);
}
this.next();
}, {
only: ["post"]
});
@garrilla
Copy link

Router.onBeforeAction(function(){
    var postSlug = this.params.slug;
    var currentPost = Posts.findOne({slug: postSlug});

    var anonymousSession = "anonymousViewers_"+currentPost._id;
    var anonymousCount = Session.keys[anonymousSession]; // ** change here
    var sessionObject = {};

    if(anonymousCount){
        sessionObject[anonymousSession] = parseInt(anonymousCount) + 1
    }else{
        sessionObject[anonymousSession] = parseInt(anonymousCount) + 1
    }
    Session.set(sessionObject);

    this.next();
}, {
    only: ["post"]
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment