-3

In this Meteor app I have a Meteor collection and a few subscribe calls that are supposed to fill it with at least one record published by the server. Moreover, there are iron-router wait() calls on it. However, the collection never gets populated. The server does send the record, because another collection, to which the client had subscribed beforehand, receives it.

Issue is also on GitHub, in case it turns out to be a bug.

Dan Dascalescu
  • 143,271
  • 52
  • 317
  • 404
  • Isnt this the expected behaviour? Its run again so that the .wait() is checked to see whether the subscription is complete. The data type stuff (peeking into collections) goes in the `data` key where it wont run again unless there is a reactive change. – Tarang Feb 18 '14 at 13:13
  • Could you include an example with code? – hharnisc Feb 18 '14 at 16:05
  • @hharnisc: I had linked to the repo in the first line of my question? – Dan Dascalescu Feb 18 '14 at 16:17

1 Answers1

1

I think there might be 2 problems.

The first is that the published data ends up in the collection, Books, on the client. That is the collection it is in on the server and that is the collection it will end up in on the client if the publish returns a cursor. Have a close look at the counts-by-room example in the docs and see how publish subscribe names match but that collection names need to be set with custom 'added(), remove(), changed() blocks to manage a collection of a different name on the client than the server.

The second problem is in how you are checking for the data on the client. Iron-router waits for the subscribe handle's ready() to return true. But that true just means that the server has sent all the data. From the docs on ready()- "True if the server has marked the subscription as ready. A reactive data source."

So the server has sent all the data but it might not yet be on the client. So you always need to check that the data you want is there and never assume that all the data has arrived just because the subscription is ready().

Try adding this to your client code:

Deps.autorun( function(){
   // replace this.params.name with the name because it is no longer in scope
   console.log( Books.findOne( {name: this.params.name} )); 

});

And I think it will log the data you are looking for soon after the subscribe is ready.

user728291
  • 4,138
  • 1
  • 23
  • 26
  • Regarding the first problem - exactly, the collection names on the client and the server must match. I've just spend the past hour or two writing this answer on [how collections, subscriptions and publications work in Meteor](http://stackoverflow.com/questions/19826804/understanding-meteor-publish-subscribe/21853298#21853298), and realized the same thing. – Dan Dascalescu Feb 18 '14 at 13:15