javascript - 如何遍历 Meteor 模板中的所有现有用户?

标签 javascript meteor iron-router publish-subscribe

我试图让所有用户在我的主页模板上进行迭代,但我在让它工作时遇到了麻烦。我一直在尝试很多不同的技术,但这就是我现在所拥有的:

服务器:

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/

相关文章:

javascript - 在运行任何其他 JavaScript 之前在 UIWebView 中运行 JavaScript

javascript - 如何检测浏览器是否支持gesturestart事件?

javascript - 在浏览器控制台上为未经身份验证的用户禁用 Meteor Router.routes

javascript - 如何在 Handlebars 模板中初始化 jQuery 小部件?

javascript - 在 req.body 中包含表

javascript - Twitter Bootstrap 模式淡入并在手动切换半秒后消失

javascript - Meteor:检查用户是否阅读消息

javascript - meteor js中没有服务的用户身份验证

javascript - iron-router notFound 模板在 meteor 中不起作用

javascript - 当订阅已经限制了数据集时,是否有必要将限制传递给数据?