【问题标题】:Iterating through user accounts in meteor accounts UI在流星帐户 UI 中迭代用户帐户
【发布时间】:2017-11-20 14:43:35
【问题描述】:

我想以表格格式显示所有用户信息,作为管理页面的一部分。我也使用了流星帐户 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)没有显示所有用户的信息。

谢谢。

【问题讨论】:

  • 你能展示你的出版物吗?这就是将数据传递给客户端以显示的内容。
  • @MichelFloyd 我还没有创建出版物。出版物在这里有什么用处?如何为这种情况定义出版物?我是流星新手。

标签: meteor meteor-blaze meteor-accounts


【解决方案1】:

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

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}}

【讨论】:

  • 感谢您帮助我。现在,即使在删除自动发布包之后,该应用程序也会返回用户信息。此外,要获取电子邮件地址,它应该是:&lt;td&gt;{{emails.[0].address}}&lt;/td&gt;
【解决方案2】:

多处出错:

  1. Meteor.users() 只会在您发布多个用户(或使用autopublish)时为您提供多个用户。

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-11
    • 2021-06-05
    • 1970-01-01
    • 2017-07-18
    • 1970-01-01
    • 2016-11-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多