遍历流星帐户中的用户帐户 UI

Iterating through user accounts in meteor accounts UI

我想以表格格式显示所有用户信息作为管理页面的一部分。我使用了 meteor accounts ui 包。

HTML代码是:

{{#each userList}}
<tbody>
  <tr>
    <th scope="row">*</th>
    <td>{{infofullname}}</td>
    <td>{{infosurname}}</td>
    <td>{{infoemail}}</td>
  </tr>

</tbody>
{{/each}}

问题是显示的是当前用户的信息,而不是所有已注册用户的信息。迭代确实发生,但对于当前登录的用户。也没有显示电子邮件地址。

助手代码是:

Template.students.helpers({
userList: function(){  
return Meteor.users.find({});
},

infofullname: function(){
return Meteor.user().profile.fullname;
},

infosurname: function(){
return Meteor.user().profile.surname; 
},

infoemails: function(){
return Meteor.user().emails.[0].address;
}
});

我面临以下问题: 1)电子邮件地址未显示。 2)没有显示所有用户的信息。

谢谢。

多处错误:

  1. Meteor.users() 只有在您发布它们(或您使用 autopublish)时才会给您多个用户。

  2. Meteor.user() 将始终只为您提供当前登录的用户。所以你所有的助手都不会按照你的计划工作。修改它们以使用 Meteor.users.findOne({_id: id)})。您始终可以使用带参数的助手。

  3. Meteor 默认只发布 profile 而不是 emails。因此,您必须在出版物中发布 emails 字段。

在服务器上发布具有以下内容的所有用户:

Meteor.publish('allUsers',function(){
  return Meteor.users.find({},{fields: {emails: 1, profile: 1}});
  this.ready();
});

然后在客户端订阅:

Meteor.subscribe('allUsers');

您的助手将需要按照@Sudhanshu 的建议进行一些细微的修改,但是由于您在用户游标上循环,您可以利用 this 作为循环内的单个用户对象。

Template.students.helpers({
  userList() {  
    return Meteor.users.find({});
  },

  infofullname() {
    return this.profile.fullname;
  },

  infosurname() {
    return this.profile.surname; 
  },

  infoemails: function(){
    return this.emails.[0].address;
  }
});

您还可以直接在 blaze 中访问嵌套属性,避免需要三个助手,例如:

{{#each userList}}
<tbody>
  <tr>
    <th scope="row">*</th>
    <td>{{profile.fullname}}</td>
    <td>{{profile.surname}}</td>
    <td>{{emails.[0].address}}</td>
  </tr>
</tbody>
{{/each}}