Meteor.user()在页面重新加载后返回未定义 [英] Meteor.user() returns undefined after page reload

查看:53
本文介绍了Meteor.user()在页面重新加载后返回未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Thing is I want to check if the user is is logged in via Meteor.user() within onBeforeAction in my routes. Problem is, after a page reload Meteor.user() returns undefined for a split second before it gets loaded.

这是我的路线配置:

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();"搜索了很多内容和变通方法在我的情况下似乎不起作用. 同样有趣的是...在本地它可以很好地工作,因此我可以重新加载页面,仍然停留在列表"视图中,但是在Modulus部署的应用程序的行为如上所述,并重定向到登录页面.

I googled a lot and the workarounds with "waitOn" and "return Meteor.user();" doesn't seem to work in my case. Also interesting...locally it works perfectly fine, so I can do a page reload and still stay on the "list" view, but the on Modulus deployed app acts as described above and redirects to the login page.

有什么想法吗?预先感谢.

Any ideas? Thanks in advance.

推荐答案

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

It's better to use !!Meteor.userId() to check if a user is logged in, because there's no latency while waiting for the user data to be loaded from the server. In fact, Meteor.user routine is more or less equivalent to:

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

解释了为什么即使用户登录它也可能返回undefined的原因.

which explains why it may return undefined even if the user is logged in.

这篇关于Meteor.user()在页面重新加载后返回未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆