Meteor.user()在页面重新加载后返回undefined

Mat*_*sch 7 meteor iron-router

我想检查用户是否通过我的路线中的onBeforeAction内的Meteor.user()登录.问题是,在页面重新加载之后,Meteor.user()在加载之前会在一瞬间返回undefined.

这里我的路线配置:

Router.map(function() {
    this.route('list', {
        path: '/list',
        template: 'list',
        onBeforeAction: function(){
            console.log('onBeforeAction');
            if(!Meteor.user()){
                 Router.go('/login');
            }
            this.next();
        }
    });
});
Run Code Online (Sandbox Code Playgroud)

我搜索了很多,并使用"waitOn"和"返回Meteor.user();"进行了解决方法.似乎在我的情况下不起作用.同样有趣...本地它工作得非常好,所以我可以重新加载页面并仍然保持在"列表"视图中,但是模块上部署的应用程序如上所述并重定向到登录页面.

有任何想法吗?提前致谢.

Tom*_*cik 7

最好用于!!Meteor.userId()检查用户是否已登录,因为在等待从服务器加载用户数据时没有延迟.事实上,Meteor.user例程或多或少等同于:

function () {
  return Meteor.users.findOne({ _id: Meteor.userId() });
}
Run Code Online (Sandbox Code Playgroud)

这解释了undefined即使用户登录也可能返回的原因.