Meteor.user() returns 页面重新加载后未定义

Meteor.user() returns undefined after page reload

我想检查用户是否通过 Meteor.user() 在我的路线的 onBeforeAction 中登录。 问题是,在页面重新加载后 Meteor.user() returns 在加载之前的一瞬间未定义。

这是我的路线配置:

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

我在谷歌上搜索了很多,"waitOn" 和 "return Meteor.user();" 的解决方法似乎对我的情况不起作用。 同样有趣...在本地它工作得很好,所以我可以重新加载页面并仍然停留在 "list" 视图中,但是在 Modulus 上部署的应用程序如上所述并重定向到登录页面。

有什么想法吗?提前致谢。

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

function () {
  return Meteor.users.findOne({ _id: Meteor.userId() });
}

这解释了为什么它可能 return undefined 即使用户已登录。