0

I am currently trying to log user page views in meteor app by storing the userId, Meteor.Router.page() and timestamp when a user clicks on other pages.

//userlog.js
Meteor.methods({
  createLog: function(page){
    var timeStamp = Meteor.user().lastActionTimestamp;
    //Set variable to store validation if user is logging in
    var hasLoggedIn = false;
    //Checks if lastActionTimestamp of user is more than an hour ago
    if(moment(new Date().getTime()).diff(moment(timeStamp), 'hours') >= 1){
      hasLoggedIn = true;
    }
      console.log("this ran");

    var log = {
      submitted: new Date().getTime(),
      userId: Meteor.userId(),
      page: page,
      login: hasLoggedIn
    }

    var logId = Userlogs.insert(log);

    Meteor.users.update(Meteor.userId(), {$set: {lastActionTimestamp: log.submitted}});
    return logId;
  }
});

//router.js This method runs on a filter on every page
'checkLoginStatus': function(page) {
    if(Meteor.userId()){
      //Logs the page that the user has switched to
      Meteor.call('createLog', page);
      return page;
    }else if(Meteor.loggingIn()) {
      return 'loading';
    }else {
      return 'loginPage';
    }
  }

However this does not work and it ends up with a recursive creation of userlogs. I believe that this is due to the fact that i did a Collection.find in a router filter method. Does anyone have a work around for this issue?

0

1 Answer 1

1

When you're updating Meteor.users and setting lastActionTimestamp, Meteor.user will be updated and send the invalidation signal to all reactive contexts which depend on it. If Meteor.user is used in a filter, then that filter and all consecutive ones, including checkLoginStatus will rerun, causing a loop.

Best practices that I've found:

  1. Avoid using reactive data sources as much as possible within filters.

  2. Use Meteor.userId() where possible instead of Meteor.user()._id because the former will not trigger an invalidation when an attribute of the user object changes.

  3. Order your filters so that they run with the most frequently updated reactive data source first. For example, if you have a trackPage filter that requires a user, let it run after another filter called requireUser so that you are certain you have a user before you track. Otherwise if you'd track first, check user second then when Meteor.logginIn changes from false to true, you'd track the page again.

This is the main reason we switched to meteor-mini-pages instead of Meteor-Router because it handles reactive data sources much easier. A filter can redirect, and it can stop() the router from running, etc.

Lastly, cmather and others are working on a new router which is a merger of mini-pages and Meteor.Router. It will be called Iron Router and I recommend using it once it's out!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.