Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changing filters reactively #10

Open
knoid opened this issue Sep 29, 2016 · 3 comments
Open

Changing filters reactively #10

knoid opened this issue Sep 29, 2016 · 3 comments

Comments

@knoid
Copy link

knoid commented Sep 29, 2016

First of all, the package looks great!

I know this is a weird error and I'll give you two very similar scripts, the first one works, the second doesn't.

In this case, the publication takes in the filters by which it searches the products.

PublishRelations('products', function(filters) {
  if (!this.userId || !Meteor.users.findOne(this.userId)) {
    return this.ready();
  }
  this.cursor(Products.find(filters));
  this.ready();
});

This works as expected.

In this other case, the filters get read from the user document. I update the user's filters via a Meteor.method and they are updated correctly. This publish gives the correct products to the client, but upon changing filters, those products that did much and now doesn't, don't get removed and also don't get updated reactively when they change.

PublishRelations('products', function() {
  if (!this.userId || !Meteor.users.findOne(this.userId)) {
    return this.ready();
  }
  this.cursor(Meteor.users.find(this.userId, { limit: 1 }), function(id, doc) {
    this.cursor(Products.find(doc.filters));
  });
  this.ready();
});

Everything gets fixed when refreshing the page so this looks related to the users collection. Does it behave differently from a normal collection?

Any ideas? Hope this helps.

@lfades
Copy link
Owner

lfades commented Sep 29, 2016

try this:

this.cursor(Meteor.users.find(this.userId, { limit: 1 }), function(id, doc) {
  if (doc.filters) {
    this.cursor(Products.find(doc.filters));
  }
});

the problem can be when a different field is updated in the user, then if you update for example the username doc will only have the username and not the entire document so doc.filters will be undefined (I said that in the documentation but maybe I'm not as good doing documentation :'c).

The previous behavior is the expected because you only want to restart the products query when changing the filters field.

According to the above, the following code can also work, since only receive updates when filters change and is better if you don't need more user fields.

this.cursor(Meteor.users.find(this.userId, { limit: 1, fields: {filters: 1} }), function(id, doc) {
  this.cursor(Products.find(doc.filters));
});

@knoid
Copy link
Author

knoid commented Sep 30, 2016

Ohh ok, I didn't understand that from the docs but anyway I was only changing the filters and I ended up console.log-ing the filters and it was never undefined. So, no idea why that happens.

@lfades
Copy link
Owner

lfades commented Sep 30, 2016

check the update you are doing on the user, I'm not quite sure how to help there :S

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants