Mike
Mike

Reputation: 1230

How to publish data based on URL in Meteor with Flow Router

I'm trying to publish data specific to the author of a document in my Jobs collection. My route is setup specifically to each unique author, which I then get via FlowRouter.getParam, but it still does not produce any data. I am subscribed to the 'refiJobs' publication but I'm still struggling. Thanks for reading - help is much appreciated!

My Publication

Meteor.publish('refiJobs', function () {
  if (Roles.userIsInRole(this.userId, 'admin')) {
    var author = FlowRouter.getParam('author');
    return Jobs.find({author: author});
  } else {
    this.error(new Meteor.Error(403, "Access Denied"));
  }
});

My route:

authenticatedRoutes.route( '/admin/:author', {
  action: function() {
    BlazeLayout.render( 'default',  { yield: 'user' } );
  }
});

Upvotes: 0

Views: 146

Answers (1)

Michel Floyd
Michel Floyd

Reputation: 20226

The route parameters are not directly available on the server where you are creating your publication. You need to pass your route parameter through to your publication via your subscription as follows:

Client:

Meteor.subscribe('refiJobs',FlowRouter.getParam('author'));

Server:

Meteor.publish('refiJobs',(author)=>{
  check(author,String); // be sure to check the parameter(s) to your publication
  if (Roles.userIsInRole(this.userId, 'admin')) {
    return Jobs.find({author: author});
  } else {
    this.error(new Meteor.Error(403, "Access Denied"));
  }
}); 

Upvotes: 1

Related Questions