如何使FlowRouter等待客户端上的用户收集 [英] How to make FlowRouter wait for users collection on the client

查看:128
本文介绍了如何使FlowRouter等待客户端上的用户收集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写我的应用程序的一部分,要求用户具有操作员"角色. 我正在FlowRouter的triggersEnter函数中进行检查.我希望没有操作员角色的用户显示受限访问页面.

I'm writing a section of my app which requires the user to have the 'operator' role. I'm checking for this in FlowRouter's triggersEnter function. I want that the user which doesn't have the operator role to be shown a restricted access page.

我使用FlowRouter,Roles和brettle:accounts-deluxe这些功能会自动以访客身份登录每位访客.

I use FlowRouter, Roles and brettle:accounts-deluxe which auto logs in as guest every visitor.

这是我的代码,routes.js:

FlowRouter.route('/switchboard', {
  name: 'switchboard',
  triggersEnter: [function (context, redirect, stop) {
    if (!Roles.userIsInRole(Meteor.userId(), ['operator'])) {
      BlazeLayout.render('main', {
        content: 'restrictedAccess'
      });           
      stop();
    }
  }],
  action: function () {
    BlazeLayout.render('main', {
        content: 'switchboard'
    });
  }
});

一切都可以在localhost上正常运行,但是在服务器上使用mup部署应用程序时,运行triggersEnter时,Meteor.user()undefined(Meteor.userId()返回确定). ,并且Roles.userIsInRole调用的结果为false,尽管在​​数据库中查看该用户显然具有操作员角色.

Everything works as expected on localhost, but when the app is deployed using mup, on a server, at the time triggersEnter is ran, Meteor.user() is undefined (Meteor.userId() returns ok), and the result of Roles.userIsInRole call is false, although looking in the database it's clear the user has the operator role.

我认为在triggersEnter运行时,用户订阅不可用,这意味着用户集合未在客户端上发布.我有这种感觉,因为如果我通过单击链接访问路由,则userIsInRole的结果是可以的,但是如果刷新页面,则会出现所描述的问题. 我想知道为什么仅在服务器上发生这种情况,以及如何解决它.

I think the users subscription is not available at the time triggersEnter is ran meaning that the users collection is not published on the client. I have this feeling because if i access the route by clicking on a link the userIsInRole result is ok, but if I refresh the page I get the problem described. I would like to know why is this happening only on the server and how can I fix it.

推荐答案

原因是FlowRouter triggersEnter不会阻止模板呈现,它会在预订Roles集合之前检查角色.解决方案是在应用程序初始化上使用FlowRouter.wait(),然后对Roles进行全局订阅(您需要全局订阅-而不是与模板级别绑定)集合,并在准备就绪时调用FlowRouter.initialize().

The reason is that FlowRouter triggersEnter is not blocking templates from rendering and it makes checking roles before Roles collections are subscribed. Solution is to use FlowRouter.wait() on app init and then make the global subscription for Roles (you need it global - not tied to template level) collection and call FlowRouter.initialize() when its ready.

这样,FlowRouter将等待您的收集,并在准备检查后将对其进行初始化.

That way FlowRouter will wait for your collection and will be initialized after it's ready to check.

更新

Update

在localhost上,本地数据库和应用程序之间的延迟要少得多.部署应用程序后,客户端需要更多时间才能从数据库中获取数据.结果,在localhost上,当FlowRouter初始化时您的集合已准备就绪,而在已部署的应用程序中则尚未准备好.

On localhost there is much less latency between local db and app. When your app is deployed is takes more time for client to fetch data from database. In result on localhost your collection is ready when FlowRouter initializes and on deployed app it isn't.

这篇关于如何使FlowRouter等待客户端上的用户收集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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