If statement Meteor:仅当角色==';网站管理员';

If statement Meteor:仅当角色==';网站管理员';,if-statement,meteor,handlebars.js,roles,If Statement,Meteor,Handlebars.js,Roles,我希望显示基于在页面上迭代的用户的信息 我有以下模板: <template name="allUsers"> <div> {{#each user}} {{username}}<br/> <!-- This is where I need some guidance --> {{#if roles == 'webmaster}} <!-- What's the correct way to do

我希望显示基于在页面上迭代的用户的信息

我有以下模板:

<template name="allUsers">
  <div>
    {{#each user}}
      {{username}}<br/>
      <!-- This is where I need some guidance -->
      {{#if roles == 'webmaster}} <!-- What's the correct way to do this? -->
        This user is a webmaster
      {{/if}}
    {{/each}}
  </div>
</template>

{{{#每个用户}}
{{username}}
{{#如果角色=='webmaster} 此用户是网站管理员 {{/if} {{/每个}}
我正在使用alanning:roles包,并且在服务器中,我已经确保为用户发布roles字段。角色。[0]现在将返回网站管理员,角色。[1]返回管理员。。。但我知道,随着角色的增加,它不会有0=webmaster和1=admin的标准密钥

有没有办法只测试角色数组是否包含要在客户端上显示的“网站管理员”?

尝试以下方法:

<template name="allUsers">
  <div>
    {{#each user}}
      {{username}}<br/>
      <!-- This is where I need some guidance -->
      {{#if roles.[1] == 'webmaster}} <!-- What's the correct way to do this? -->
        This user is a webmaster
      {{/if}}
    {{/each}}
  </div>
</template>

{{{#每个用户}}
{{username}}
{{{#如果角色。[1]='webmaster} 此用户是网站管理员 {{/if} {{/每个}}
这里有一个全局模板帮助器,可以帮助您解决此问题

Template.registerHelper( 'isUserInRole', function( userId, role ) {
    return Roles.userIsInRole(userId, role);
});

<template name="allUsers">
  <div>
    {{#each user}}
      {{username}}
      {{#if isUserInRole _id 'webmaster'}}
        This user is a webmaster
      {{/if}}
    {{/each}}
  </div>
</template>
Template.registerHelper('isUserInRole',函数(用户ID,角色){
返回Roles.userIsInRole(userId,role);
});
{{{#每个用户}}
{{username}}
{{{#如果isUserInRole{u id'webmaster'}
此用户是网站管理员
{{/if}
{{/每个}}

**根据第一条评论进行编辑。

这是指当前登录的用户。我的问题是如何显示每个迭代用户的角色。此代码将显示每个用户都有“网站管理员”角色,因为我的帐户设置为“网站管理员”。因此,如果我理解您的问题,您希望访问数组的特定元素。您可以尝试找到正确的语法(我非常确定stackoverflow中有这样一个问题)来检查特定的元素。我认为语法是user.roles[0]或user.roles[1]。请尝试我将在回答中包含的代码在本例中。。。如果一个用户的角色是['webmaster','admin'],而另一个用户的角色是['admin','webmaster'],该怎么办。你的答案没有说明为什么它应该工作。与S.19LabG最初发布的内容相同:这指的是当前登录的用户。我的问题是如何显示每个迭代用户的角色。此代码将显示每个用户都具有“网站管理员”角色,因为我的帐户设置为“网站管理员”。