iron-meteor / iron-router

A client and server side router designed specifically for Meteor.
MIT License
1.98k stars 413 forks source link

Meteor User profile undefined error during iron router's logout #1508

Closed emehmet closed 8 years ago

emehmet commented 8 years ago

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.

chrisbutler commented 8 years ago

@emehmet I'm not sure i totally understand your question or what you're trying to accomplish, but Meteor.user() is reactive, so the workList helper gets re-run when you log in or out, because the user is changing.

to avoid the error, you need to check whether there is a user first, before trying to get their profile, like this (called adding a 'guard'):

Meteor.user() && Meteor.user().profile