Matthew Slaton
Matthew Slaton

Reputation: 349

Having issues with showing collection data in meteor

I am having collection issues in my Meteor project. I added the following code to my Resolutions.js file:

Resolutions = new Mongo.Collection('resolutions');

if (Meteor.isClient) {
  Template.body.helpers({
resolutions: function() {
  Resolutions.find({});
}
});

Template.body.events({
'submit .new-resolution': function(event) {
  var title = event.target.title.value;

  Resolutions.insert({
    title: title,
    createdAt: new Date()
  });

  event.target.title.value = "";

  return false;
 }
});
}

if (Meteor.isServer) {
 Meteor.startup(function () {
 // code to run on server at startup
});
}

and added the following to my Resolutions.html file:

<head>
  <title>Resolutions</title>
</head>

<body>
  <div class="container">
    <header>
      <h1>Monthly Resolutions</h1>

      <form class="new-resolution">
        <input type="text" name="title" placeholder="A new resolution" />
        <input type="submit" value="Submit"/>
      </form>
    </header>

    <ul>
      {{#each resolutions}}
        {{> resolution}}
      {{/each}}
    </ul>
  </div>
</body>

<template name="resolution">
  <li>{{title}}</li>
</template>

After running the app I get zero errors but still the site does not return any of the collection value. I have no clue what is wrong with this project.

Upvotes: 0

Views: 69

Answers (1)

Template.body.helpers({
    resolutions: function() {
        return Resolutions.find({});
    }
});

You forgot to use the return keyword.Remember that when creating a helper, you always need to return a value.

Upvotes: 4

Related Questions