meteor-collections

How to return a collection in code from a Meteor project created with meteor-boilerplate?


This is the code I am using:

Contacts = new Mongo.Collection('contacts');
Template.contact.helpers({
  contact: function() {
  return Contacts.find({});
  }
});

However the HTML is not returning the collection.


Solution

  • If you look at the meteor-boilerplate website, you can see that

    "insecure" and "autopublish" are removed by default!

    By default, Meteor includes the autopublish package which makes all data in the database available to the client. This is only suitable for early development, and any real project will remove it. So meteor-boilerplate removes it by default.

    Without autopublish, you will need to publish the data yourself. You can try this:

    // server code
    Meteor.publish("contacts", function () {
        return Contacts.find();
    });
    
    // client code
    Meteor.subscribe("contacts");
    

    Then your existing code should work.

    For more information, see publish and subscribe from the Meteor docs.