【问题标题】:How to iterate through all existing users in a Meteor template?如何遍历 Meteor 模板中的所有现有用户?
【发布时间】:2016-03-26 16:01:50
【问题描述】:

我正在尝试让所有用户在我的主页模板上进行迭代,但我无法让它正常工作。我一直在尝试很多不同的技术,但这就是我现在所拥有的:

服务器:

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}} 块,因为我不知道该在那里调用什么。甚至没有显示测试文本。

【问题讨论】:

    标签: javascript meteor iron-router publish-subscribe


    【解决方案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>
    

    【讨论】:

      猜你喜欢
      • 2015-03-16
      • 1970-01-01
      • 2015-06-13
      • 1970-01-01
      • 2018-04-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-08
      相关资源
      最近更新 更多