David Hatton
David Hatton

Reputation: 347

Meteor: Iterating over mongoDB collection produces no results

I have a collection in Mongo that is populated with three records.

When viewing my app in localhost, it lists nothing. There are no errors displayed, however nothing is listed either.

What am I doing wrong?

Console output from manual find:

meteor:PRIMARY> db.users.find({})
{ "_id" : ObjectId("55ddba705374fcb03117d585"), "username" : "coderboy", "joined" : ISODate("2015-08-26T13:09:04.872Z") }
{ "_id" : ObjectId("55ddc7475374fcb03117d586"), "username" : "plumberboy", "joined" : ISODate("2015-08-26T14:03:51.960Z") }
{ "_id" : ObjectId("55ddcf3b5374fcb03117d587"), "username" : "sparkieboy", "joined" : ISODate("2015-08-26T14:37:47.883Z") }

HTML:

<body>
    <div class="container">
        <h2>Our users:</h2>
        <ul>
            {{#each user}}
                {{> userlist}}
            {{/each}}
        </ul>
    </div>
</body>

<template name="userlist">
    <li>{{username}}</li>
</template>

JS:

users = new Mongo.Collection("users");

if (Meteor.isClient) {
    Template.userlist.helpers({
            user: function () {
               return users.find({});
            }
    });
}

Upvotes: 0

Views: 45

Answers (1)

Christian Fritz
Christian Fritz

Reputation: 21384

You use the user helper of userlist outside of the userlist template. So that's not defined. An easy fix is put move the each into the userlist template.

<body>
    <div class="container">
        <h2>Our users:</h2>
        <ul>
            {{> userlist}}
        </ul>
    </div>
</body>

<template name="userlist">
    {{#each user}}
      <li>{{username}}</li>
    {{/each}}
</template>

Upvotes: 1

Related Questions