Display all users in Meteor

I have a template that I am trying to display all users in the called userList.

//server

Meteor.publish("userList", function() {

var user = Meteor.users.findOne({
    _id: this.userId
});


if (Roles.userIsInRole(user, ["admin"])) {
    return Meteor.users.find({}, {
        fields: {
            profile_name: 1,
            emails: 1,
            roles: 1
        }
    });
}

this.stop();
return;
});

Thanks in advance for your help!

+4
source share
3 answers

if you want to show all the users you can try in the publish.js file:

Meteor.publish('userList', function (){ 
  return Meteor.users.find({});
});

in your router that you are subscribing to this

Router.route('/users', {
    name: 'usersTemplate',
    waitOn: function() {
        return Meteor.subscribe('userList');
    },
    data: function() {
        return Meteor.users.find({});       
    }
 });

The next step is to iterate your data in the template.

If you do not want to subscribe to the router, you can subscribe at the template level, please read this article for more details.

https://www.discovermeteor.com/blog/template-level-subscriptions/

Sincerely.

+11
source

That should work!

//on server

    Meteor.publish("userList", function () {
           return Meteor.users.find({}, {fields: {emails: 1, profile: 1}});
    });

// in the client

    Meteor.subscribe("userList");
+6

.

  • ()
  • ()

:

UserListCtrl = RouterController.extend({
    template: 'UserList',
    subscriptions: function () {
       return Meteor.subscribe('users.list', { summary: true });  
    },
    data: function () {
       return Meteor.users.find({});
    }
});

:

Meteor.publish('users.list', function (options) {
    check(arguments, Match.Any);
    var criteria = {}, projection= {};
    if(options.summary){
       _.extend(projection, {fields: {emails: 1, profile: 1}});
    }
    return Meteor.users.find(criteria, projection);
});
0

All Articles