我想以表格格式显示所有用户信息,作为管理页面的一部分。我使用了流星帐户 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(所有用户的信息都没有显示。
谢谢。
在服务器上发布具有以下内容的所有用户:
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}}
多件事是错误的:
-
Meteor.users(( 只有在您发布用户(或使用
autopublish
时才会为您提供多个用户。 -
Meteor.user(( 将始终只为您提供当前登录的用户。因此,您的所有助手都不会按照您的计划工作。修改它们以使用
Meteor.users.findOne({_id: id)})
。始终可以将帮助程序与参数一起使用。 -
默认情况下,Meteor 只发布
profile
,而不发布emails
。因此,您必须在出版物中发布emails
字段。