如何遍历 Meteor 模板中的所有现有用户?
How to iterate through all existing users in a Meteor template?
我试图让所有用户都在我的主页模板上进行迭代,但我无法让它工作。我一直在尝试很多不同的技术,但这就是我现在所拥有的:
服务器:
Meteor.publish('userList', function() {
return Meteor.users.find({}, {fields: {username: 1, emails: 1, profile: 1}});
});
路由:
Router.route('/', {
name: 'home',
template: 'home',
waitOn: function() {
return Meteor.subscribe('userList');
},
data: function() {
return Meteor.users.find({});
}
});
HTML:
<template name="home">
<h1>Home page</h1>
{{#each userList}}
<p>Test</p>
{{userList.username}}
{{/each}}
</template>
我认为我的问题实际上出在 {{#each}}
块中,因为我不知道在那里调用什么。连测试文本都不显示。
解决问题的一种方法是 return {userList: Meteor.users.find()}
在 data
函数中:
Router.route('/', {
name: 'home',
template: 'home',
waitOn: function() {
return Meteor.subscribe('userList');
},
data: function() {
return {userList: Meteor.users.find()};
}
});
然后,您可以通过将 home
模板更改为:
来遍历 userList
<template name="home">
<h1>Home page</h1>
{{#each userList}}
<p>Test</p>
{{username}}
{{/each}}
</template>
我试图让所有用户都在我的主页模板上进行迭代,但我无法让它工作。我一直在尝试很多不同的技术,但这就是我现在所拥有的:
服务器:
Meteor.publish('userList', function() {
return Meteor.users.find({}, {fields: {username: 1, emails: 1, profile: 1}});
});
路由:
Router.route('/', {
name: 'home',
template: 'home',
waitOn: function() {
return Meteor.subscribe('userList');
},
data: function() {
return Meteor.users.find({});
}
});
HTML:
<template name="home">
<h1>Home page</h1>
{{#each userList}}
<p>Test</p>
{{userList.username}}
{{/each}}
</template>
我认为我的问题实际上出在 {{#each}}
块中,因为我不知道在那里调用什么。连测试文本都不显示。
解决问题的一种方法是 return {userList: Meteor.users.find()}
在 data
函数中:
Router.route('/', {
name: 'home',
template: 'home',
waitOn: function() {
return Meteor.subscribe('userList');
},
data: function() {
return {userList: Meteor.users.find()};
}
});
然后,您可以通过将 home
模板更改为:
userList
<template name="home">
<h1>Home page</h1>
{{#each userList}}
<p>Test</p>
{{username}}
{{/each}}
</template>