我试图让所有用户在我的主页模板上进行迭代,但我在让它工作时遇到了麻烦。我一直在尝试很多不同的技术,但这就是我现在所拥有的:
服务器:
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}}
block ,因为我不知道在那里调用什么。甚至测试文本也不显示。
最佳答案
解决问题的一种方法是在 data
中返回 {userList: Meteor.users.find()}
功能:
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>
关于javascript - 如何遍历 Meteor 模板中的所有现有用户?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34381118/