I just started using meteor today and can’t seem to figure out what I’m doing wrong. I have a query that is being run inside of a publish function, but this query is filtered by the result of another query.
In short, when I add a document to the database that is being published (CollectionTwo) it works as I would expect, but when I make my change in the database that is being used to filter (CollectionOne), meteor doesn’t behave reactively.
CollectionOne = new Meteor.Collection("one")
CollectionTwo = new Meteor.Collection("two")
Meteor.publish("items", ->
not_hidden = CollectionOne.find().fetch()
return CollectionTwo.find( _id: {'$in':( t.my_id for t in not_hidden )} )
)
Meanwhile, on the client…
CollectionOne = new Meteor.Collection("one")
CollectionTwo = new Meteor.Collection("two")
Meteor.subscribe("items")
_.extend( Template.items,
items: ->
not_hidden = CollectionOne.find().fetch()
return CollectionTwo.find( _id: {'$in':( t.my_id for t in not_hidden )} )
)
Any ideas what the appropriate solution might be?
Reactivity doesn’t work that way inside
Meteor.publishon the server. Meteor won’t recalculate theCollectionTwo.findquery when the contents ofCollectionOnechanges.To implement what you want, manage the publish by hand, instead of just returning a Cursor. You’ll need to use
observeinside your publish function to watch for changes onCollectionOne, and then manually callthis.setandthis.unsetto push changes down to the client. There’s an example of this technique in the publish documentation. The example only looks at one collection, but you can extend the idea to a nested set of observes.We’re going to work on sugar to make this sort of pattern easier to implement.