javascript - Waiting on Meteor.user() - Stack Overflow

If a new users signs up, I take them to the getting started route so they enter can enter a name which

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'},
}
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745252964a4618786.html

相关推荐

  • javascript - Waiting on Meteor.user() - Stack Overflow

    If a new users signs up, I take them to the getting started route so they enter can enter a name which

    7小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信