Skip to content

Instantly share code, notes, and snippets.

@tsega
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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"]
});
@tsega
Copy link
Author

tsega commented Jan 21, 2015

When I add the lines 8-12 my application just hangs. I just can't understand what I'm doing wrong.

@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