Reactive updates when query is filtered by another query

meteor

Solution

Reactivity doesn't work that way inside `Meteor.publish` on the server. Meteor won't recalculate the `CollectionTwo.find` query when the contents of `CollectionOne` changes.

To implement what you want, manage the publish by hand, instead of just returning a Cursor. You'll need to use `observe`inside your publish function to watch for changes on `CollectionOne`, and then manually call `this.set` and `this.unset` to 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.

Problem

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?

Original source