Arcy
Arcy

Reputation: 375

Meteor publish: admin can view all data, user only his data

I have a collection (Collection2 package):

tickets = new Mongo.Collection("tickets");

I would like to show, through the template, ALL USER tickets if admin is logged in, else only current user tickets.

My server code (meteor-rules package) :

Meteor.publish('tickets', function(){
    if (Roles.userIsInRole(this.userId, ['admin'])){
        return tickets.find({},{sort:{deadline: 1}});
    }else{
        return tickets.find({_id: this.userId}, {sort:{deadline: 1}});
    }
});

My client code:

Template.dashboard.helpers({
  tickets: function () {
    Meteor.subscribe('tickets');
  });

Nothing happened on template...what did I do wrong?

Upvotes: 0

Views: 544

Answers (1)

saimeunt
saimeunt

Reputation: 22696

Your template helper must return actual data, but in your code you don't return anything, just subscribe to your tickets.

Template.dashboard.helpers({
  tickets: function () {
    // return a cursor fetching all tickets that were pushed to the client
    // thanks to the subscription
    return tickets.find();
  }
});

Depending on your needs, you may want to use iron:router to display your dashboard only when the subscription is ready, or else it will be displayed unpopulated at first.

Upvotes: 1

Related Questions