[RESOLVED] Publish/subscribe not working - how to debug?


#1

I am trying to switch my app from autopublish to publish/subsribe. After trying to set publish/subscribe up or my first collection, none of my items from the collection are being displayed anymore, and I am at a loss at what might be going wrong.

Can someone spot a mistake in my code below or give any hints as to how to debug this type of problem?

client/routes.js

Router.route('/slug', {
  name: 'route.name',
  title: 'My Page',
  template: 'TemplateName',
  subscriptions: function() {
    this.subscribe('myPublication');
  },
  action: function() {
    this.render();
  }
});

server/lib/collectionLib.js

...
Meteor.publish('myPublication', function() {
  return MyCollection.find();
});

client/myCollection/view/mycollection-list.html

...
{{#each myCollection}}
    ...
{{/each}}
...

#2

Make sure that you declare your collection in a place where both server and client can ‘see’ it.

For example:

// both/collections.js
MyCollection = new Mongo.Collection('mycollection');

#3

you try this code…
myCollection: function (){
return Projects.find();
}


#4

@mas212: Where do you suggest I insert this code?


#5

@sergiotapia: I have my collection declared in /model/mycollection.js. My understanding is this path is made available to both client and server.


#6

Codejak, you need to expose your collection data to the template, so that the template knows what data to display.

// Use the name of your template here
Template.mycollectionlist.helpers({
    // the name of the data that will be available
    myCollection: function () {
        // You can do filtering in the find function
        return MyCollection.find();
    }
});

You should have this in the same folder place as your template, using the same name, but as a .js file.


#7

Thanks, @jorgeer, that fixed it. I guess this is the same that @mas212 suggested. Too easy, but I am still getting my head wrapped around the concept :wink:

Are there any debuggin steps that would have shown me where the problem was? Or does one simply have to know?


#8

I would recommend going through the meteor tutorial, it covers most of these basic concepts, and I wold classify this as a very basic concept, at least when using the Blaze render enginge, which is the default. :wink:
Doing it in React for example would be somewhat different.


#9

I have worked through the tutorial and referred back to it multiple times. I guess what it doesn’t achieve in conveying is what parts of different approaches/patterns are (absolutely) required, which aspects are equivalent between approaches and which ones are (optional) additions. Which probably isn’t the point of a tutorial in the first place.

I guess I now have to take this to understand that I always need a helper to expose data to a template (wording in the tutorial: “You can pass data into templates from your JavaScript code by defining helpers.”)


#10

https://www.discovermeteor.com/blog/a-guide-to-meteor-templates-data-contexts/

This blog details the multiple ways of defining the data context. A very good read, though outdated on a few points. Example being Iron:Router has been largely replaced/overtaken by kadira:flow-router, where data in the router is not a good idea.


#11

And the book this site promotes is also a fantastic introduction to meteor beyond the very basics. Well worth the investment.

If you want to specifically -debug- your subscriptions, I would definitely use https://atmospherejs.com/msavin/mongol, which allows you to see all the data available in the client. If your data is not in there, your publish/subscribe is not working.


#12

you need METEORTOYS ! its a good tool for develop in meteor
// meteor add meteortoys:allthings

the page in atmosphere https://atmospherejs.com/meteortoys/allthings

and in level UP (channel in youtube) in the intermediate meteor tutorials describe the funcion of meteorToys. (and is a exellent channel to learn of meteor).

see you and i hope this tips are helpfull.