woj
woj

Reputation: 11

How to filter a meteor cursor after the query and then publish the result

I’m trying to meteor publish only documents that a user has access to. The access system is outside of the document database but a function call like hasAccess(customuserID, _id) will return true if the user has access.

The problem is that publish returns a cursor so I can’t do something like:

Meteor.publish('theInvoices', function () {
  return Invoices.find().fetch().filter(function (doc) {
    return hasAccess(customerUserID, doc._id); // external access , filter
  });
});

Or course this functions returns an array of filtered documents and not a cursor so the question is how do I filter the results of query (without using database filters) before publishing the results or how do I turn an array of documents into a publishable cursor?

Thanks

Upvotes: 1

Views: 1479

Answers (1)

David Weldon
David Weldon

Reputation: 64312

You can try something like this:

Meteor.publish('theInvoices', function () {
  var invoices = Invoices.find().fetch().filter(function (doc) {
    return hasAccess(customerUserID, doc._id);
  });
  var invoiceIds = _.pluck(invoices, '_id');
  return Invoices.find({_id: {$in: invoiceIds}});
});

That just finds all of the allowed invoice ids, and published a new cursor based on that list. Keep in mind this isn't reactive.

Also note that customerUserID should be defined somewhere.

Upvotes: 4

Related Questions