事情是我想检查用户是否通过 Meteor.user() 在我的路由中的 onBeforeAction 中登录。
问题是,页面重新加载后 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();
}
});
});
我在谷歌上搜索了很多“waitOn”和“return Meteor.user();”的解决方法。在我的情况下似乎不起作用。
同样有趣的是......在本地它工作得很好,所以我可以重新加载页面并仍然停留在“列表” View 中,但是在 Modulus 部署的应用程序的行为如上所述并重定向到登录页面。
有任何想法吗?提前致谢。
最佳答案
最好用!!Meteor.userId()
检查用户是否登录,因为在等待用户数据从服务器加载时没有延迟。事实上,Meteor.user
例程或多或少相当于:
function () {
return Meteor.users.findOne({ _id: Meteor.userId() });
}
这解释了为什么它可能会返回
undefined
即使用户已登录。
关于Meteor.user() 在页面重新加载后返回 undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32024910/