2015-12-20 38 views
0

我想讓所有的用戶都在我的主頁模板上迭代,但我有麻煩讓它工作。我一直這麼多不同的技術,但是這是我現在得到:如何迭代流星模板中的所有現有用戶?

服務器:

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}}區塊,因爲我不知道該在那裏打電話。甚至不顯示測試文本。

回答

1

一個爲您解決問題的方法是在你的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> 
+1

這工作, 非常感謝! –