简体   繁体   中英

How to iterate through all existing users in a Meteor template?

I am trying to get all the users to be iterated on my home page template, but I'm having troubles getting it to work. I've been trying so many different techniques but this is what I've got now:

Server:

Meteor.publish('userList', function() {
  return Meteor.users.find({}, {fields: {username: 1, emails: 1, profile: 1}});
});

Routing:

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>

I think my problem actually lies in the {{#each}} block because I don't know what to call there. Not even the test text displays.

One way to solve your problem is to return {userList: Meteor.users.find()} in your data function:

Router.route('/', {
    name: 'home',
    template: 'home',
    waitOn: function() {
        return Meteor.subscribe('userList');
    },
    data: function() {
        return {userList: Meteor.users.find()};
    }
});

Then, you could iterate through userList by changing your home template to:

<template name="home">
    <h1>Home page</h1>
    {{#each userList}}
        <p>Test</p>
        {{username}}
    {{/each}}
</template>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM