admin管理员组

文章数量:1416631

If a new users signs up, I take them to the getting started route so they enter can enter a name which is at /gs. I store the name inside a name property of the profile object of the current user. Now if a user who has already entered a name and visits the /gs route I want to redirect them to the root. In iron router, I do this:

Router.route('/gs', {
  name: 'gs',
  onBeforeAction: function() {
    if ( Meteor.user().profile.name ) {
      this.redirect('/');
    } else {
      this.render();
    }
  }
});

Even though this works, it prints out 2 errors to the console. One of them being "Cannot read property 'profile' of undefined" and the lack of this.next(). Any way to fix these problems.

If a new users signs up, I take them to the getting started route so they enter can enter a name which is at /gs. I store the name inside a name property of the profile object of the current user. Now if a user who has already entered a name and visits the /gs route I want to redirect them to the root. In iron router, I do this:

Router.route('/gs', {
  name: 'gs',
  onBeforeAction: function() {
    if ( Meteor.user().profile.name ) {
      this.redirect('/');
    } else {
      this.render();
    }
  }
});

Even though this works, it prints out 2 errors to the console. One of them being "Cannot read property 'profile' of undefined" and the lack of this.next(). Any way to fix these problems.

Share Improve this question asked Dec 14, 2014 at 9:07 fardeemfardeem 5945 silver badges14 bronze badges 1
  • Try this: if ( Meteor.user() && Meteor.user().profile.name ) { – sbking Commented Dec 14, 2014 at 9:58
Add a ment  | 

2 Answers 2

Reset to default 5

Your route functions and most hooks are run in a reactive putation. This means they will rerun automatically if a reactive data source changes. For example, if you call Meteor.user() inside of your route function, your route function will rerun each time the value of Meteor.user() changes. (Iron.Router Guide: Reactivity)

Hook functions and all functions that get run when dispatching to a route are run in a reactive putation: they will rerun if any reactive data sources invalidate the putation. In the above example, if Meteor.user() changes the entire set of route functions will be run again. (Iron.Router Guide: Using Hooks)

The first time the function is run, Meteor.user() is undefined. Then its value change to an object. As it is a reactive variable, the function is run again, without error this time.

You should check if Meteor.user() is defined before using its properties. Here is a very (maybe too much) exhaustive way of doing so:

if (Meteor.user() !== undefined) {
  // the user is ready
  if (Meteor.user()) {
    // the user is logged in
    if (Meteor.user() && Meteor.user().profile.name) {
      // the name is already set
      this.redirect('/');
    } else {
      this.render();
  } else {
    // the user is not logged in
} else {
  // waiting for the user to be ready
}

Pandark has the correct answer, wanted to share my working code!

Router.route('/account',{
fastRender: true,
data:function(){

    if( Meteor.user() && Meteor.user().profile.guest ){
        Router.go('/login');
    } else {
        Router.go('/account');
    }
},
template:'screen',
yieldTemplates: {
    'account': {to: 'content'},
}
});

本文标签: javascriptWaiting on Meteoruser()Stack Overflow