【发布时间】:2018-06-05 18:26:28
【问题描述】:
在开发时,每次我保存文件时,Meteor 都会重新启动(这是一个很棒的功能),但是有些页面有一些基于用户配置文件的验证,它们会被重定向到登录页面。我正在检查,似乎 Meteor.users 还没有准备好。如何排序?
SpecialController = MainController.extend({
onBeforeAction: function(){
const user = Meteor.users.findOne({_id: Meteor.userId()});
if (user && user.profile.internalStatus == "valid") {
this.next();
} else {
// the routers is sending here but it shouldn't.
Router.go('dashboard');
}
}
});
【问题讨论】:
-
Meteor.userId 在重启时不会立即准备好,需要一小段时间才能准备好。不好的是,渲染通常已经发生了。好消息是,它是反应式的,因此您可以在其上的模板中收听或将 Tracker 与您的路由器结合使用。如果 Iron 路由器支持 before-rendered-hooks,你也可以使用它们来监听用户 ID,并且只有在 userId 出现时才继续路由。
-
userId 没问题,我的问题是 Meter.users,还没准备好。我试图使用
waitOn,但它不起作用。
标签: javascript meteor iron-router meteor-accounts