Reputation: 3335
I'm in the early stages of a Meteor project with all default packages present, including autopublish. Adding a Collection Notes
to my project worked fine and displayed as expected.
When I added a second Collection. Tags
, replicating the same pattern, it is always empty.
So I need some pointers what to watch out for. I've read e.g. David Weldons list of common mistakes but found no clue there.
There are two templates displaying two Collections, one is displaying fine, the other is not.
The Collections are declared exactly the same:
export const Tags = new Mongo.Collection('Tags');
export const Notes = new Mongo.Collection('Notes');
They are displayed by similar, simple, templates, which are referenced in my html:
<body>
<div>
{{#each tags}}
{{> tag}}
{{/each}}
</div>
<div>
{{#each notes}}
{{> note}}
{{/each}}
</div>
</body>
And the template helpers are also identical:
Template.body.helpers({
notes() {
return Notes.find({}, { sort: { createdAt: -1}});
},
tags() {
let t = Tags.find({});
return t;
},
});
If I break after let t =...
then t.fetch()
gives an empty array. (meteor mongo: db.Tags.find()
is not empty...)
Returning an array from the tags()
-helper displays that data correctly, so it is obviously something I miss with the Tags
-collection.
Might this be a case of timing? I thought that helpers are reactive and as such should take care of such things. Or do I need to look into explicit subscriptions, iron:router
and waitOn
?
FIX:
As indicated by @Jankapunkt, I had forgotten to add the collection to the server side startup.
Upvotes: 1
Views: 268
Reputation: 8423
In order to successfully autopublish your mongo collection's data to your clients, you need to import the collection both to server and client (preferably on startup).
Example
imports/MyCollection.js
export const MyCollection = new Mongo.Collection('mycollection');
server/main.js
import { MyCollection } from '../imports/MyCollection';
client/main.js
import { MyCollection } from '../imports/MyCollection';
If you remove the autopublish
package you will however need to add publications on the server side and subscriptions on the client side.
Further readings:
https://guide.meteor.com/data-loading.html
https://guide.meteor.com/collections.html
Upvotes: 1