【问题标题】:Meteor-roles (alanning). Roles.userIsInRole return false流星角色(alanning)。 Roles.userIsInRole 返回 false
【发布时间】:2016-07-21 22:32:53
【问题描述】:

我正在使用流星角色包,在 Flowrouter 上重定向时遇到问题。我在导航栏中有一个将用户重定向到主页的按钮,该按钮因角色而异。问题是角色包需要一些时间才能准备好并抛出错误。 关于如何解决这个问题的任何想法?提前致谢。

FlowRouter.route( '/', {
  name: 'home',
  triggersEnter: [() => {
    if (Meteor.userId()) {
      if (Roles.userIsInRole(Meteor.userId(), 'student')) {
        FlowRouter.go('internships_next');
      } else if (Roles.userIsInRole(Meteor.userId(), 'organization')) {
        FlowRouter.go('user_internships');
      } else {
        throw new Meteor.Error( 500,
          'There was an error processing your request. User id: ' + Meteor.userId()
        );
      }
    }
  }],
  action() {
    mount(LayoutContainer, {
      content: <LoginContainer/>,
    });
  },
});

【问题讨论】:

    标签: meteor roles flow-router


    【解决方案1】:

    在重定向之前需要确保 Roles.subscription.ready() 为真:

    FlowRouter.route( '/', {
      name: 'home',
      action() {
        Tracker.autorun(function() {
          if (Meteor.userId() && Roles.subscription.ready()) {
            if (Roles.userIsInRole(Meteor.userId(), 'student')) {
              FlowRouter.go('internships_next');
            } else if (Roles.userIsInRole(Meteor.userId(), 'organization')) {
              FlowRouter.go('user_internships');
            } else {
              mount(LayoutContainer, {
                 content: <LoginContainer/>,
              });
            }
          });
        }
      },
    });
    

    【讨论】:

    • 通过添加Roles.subscription.ready(),整个if语句为假,LoginContainer被挂载。
    • 更新我的答案请检查。
    • 它不起作用。第一个 if 语句为 false(因为 Roles.subscription.ready() 返回 false),它显示一个空白页面,因为没有安装任何内容。
    • 你等了一会儿吗?因为当 Meteor.userId() && Roles.subscription.ready() 变为 true 时,页面会重新渲染。
    • 这就是问题所在。该页面永远不会重新呈现。我认为 Flowrouter 不是反应式的,一旦它变为假,它就会保持假并且没有任何更新。所以不知道怎么处理。
    猜你喜欢
    • 2017-11-05
    • 2016-05-06
    • 2015-07-04
    • 2017-02-04
    • 1970-01-01
    • 1970-01-01
    • 2016-07-29
    • 2019-08-14
    • 1970-01-01
    相关资源
    最近更新 更多