iron Router onbeforeaction给出了2个错误,但是做了我期望它做的事情

时间:2015-03-06 14:58:30

标签: meteor

用户使用FB或Twitter登录:

我试图在这里查看多件事情,你可以看到。但由于某种原因,我得到2个错误: 1.异步函数回调中的异常:TypeError:无法读取属性' profile'未定义的 2.路线调度从未呈现过。您是否忘记在onBeforeAction中调用this.next()?

有趣的是,这段代码正在按照我的预期行事。如果profile.firsttime = false,则路由到completeSignup,如果没有登录则转到startPage。但我仍然会遇到这些错误,所以我一定做错了。

代码:

    onBeforeActions = {
    loginRequired: function() {
        if (!Meteor.userId()) {
            Router.go('startPage');
        } else  {
            if (Meteor.userId() && Meteor.user().profile.firsttime) {
                Router.go('completeSignup');
            }
        }

        this.next();
    }
};

Router.onBeforeAction(onBeforeActions.loginRequired, {
    except: ['startPage']
});

1 个答案:

答案 0 :(得分:2)

在用户文档到达客户端文档之前,

Meteor.userId()可用作登录过程的一部分。混合if中的两个实际上并不能达到你想要的效果,因为在短时间内,它们不会同时返回真值。

为了避免错误,您需要添加一些额外的guards。在else子句中尝试这样的事情:

var user = Meteor.user();
if (user && user.profile && user.profile.firsttime) {
  Router.go('completeSignup');
}