Frankie
Frankie

Reputation: 43

Shared Collection Between Client and Server Meteor

Beginner at Meteor. Just learning how everything works so bear with me.

Everything was working fine in one file, but after installing iron:router to have a multi-page application, I realized that it's better to have separate client and server files. Unfortunately, now I'm having trouble syncing the collection between the server and the client. I've read tons of tutorials, but nothing is working.

In my server.js file:

Streams = new Meteor.Collection("streams"); 
 if (Meteor.isServer) {
  Meteor.publish('streams', function () {
  return Streams.find();
 });
}

In my client.js file:

if(Meteor.isClient) {
   Meteor.subscribe("streams");
   Template.body.helpers = function(){
     return Streams.find();
 }
}

After debugging, it says that "Streams" is not defined in the client. What's going on? How do I connect the collection?

Upvotes: 4

Views: 584

Answers (3)

hosais
hosais

Reputation: 164

If you use autopublish package, which is by default. You just need to do

lib/streams.js

Streams = new Meteor.Collection("streams");

part.

Upvotes: 0

saimeunt
saimeunt

Reputation: 22696

Classic architecture :

lib/streams.js

Streams = new Meteor.Collection("streams"); 

server/streams.js

Meteor.publish("streams", function () {
  return Streams.find();
});

client/streams.js

Meteor.subscribe("streams");

Template.body.helpers({
  streams: function(){
    return Streams.find();
  }
});

Upvotes: 4

Mathieu Dutour
Mathieu Dutour

Reputation: 549

You need to define Streams on the client as well.

if(Meteor.isClient) {
   Streams = new Meteor.Collection("streams"); 
   Meteor.subscribe("streams");
   Template.body.helpers = function(){
     return Streams.find();
   }
}

Upvotes: 0

Related Questions