Search code examples
meteoriron-router

Meteor User profile undefined error during iron router's logout


I'm using Meteor.user().profile in helper. When i do logout, i gets error profile undefined. My code below:

Template.listedWork.helpers({
  workList: function() {
    if (Meteor.user().profile.yetki == 1) {
      return Work.find({})
    } else {
      return Work.find({
        username: Meteor.user().username
      });
    }

  }
});

I'm doing logout in listedWork page for example: localhost/listedWork. That is iron router render code

    Router.route('/listedWork', {
      action: function() {
          this.render('listedWork');
      },
    onBeforeAction: function() {

    if (!Meteor.userId()) {
      this.layout("loginLayout");
      this.render('login');
    } else {
      this.next();
    }
  }


     });

When i logout here, Meteor.user().profile is call by workList that's why i get this error.

login template render in onBeforeAction for logout. Why listedWork template helper call this here.

Thank you for all helps.


Solution

  • The reason Meteor.user().profile is undefined after logout is because you no longer have a Meteor.user(). You need to check for this in your helper and do an appropriate action if Meteor.user() returns null.

    Template.listedWork.helpers({
        workList() {
            if( !Meteor.user() ) {
                // Handle the case.  Redirect to /login or something.
            }
            else {
                // Your current helper code here.
            }
        }
    });