Displaying all users in Meteor
Asked Answered
S

3

6

I have a template that I am trying to display all users in 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 the help!

Sumter answered 21/6, 2015 at 4:57 Comment(1)
You can pass this.userId instead of user for example if (Roles.userIsInRole(this.userId, ['admin'])) {..}Ankylostomiasis
J
16

if you want show all the user you can try in your publish.js file:

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

in your router you susbcribe to this

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

The next step is iterate your data in the template.

if you don't want subscribe in the router, you can subscribe in template level, please read this article for more details.

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

Regards.

Jamila answered 21/6, 2015 at 5:41 Comment(1)
How do you iterate the data in the template?Canvas
P
7

This should work!

// in server

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

// in client

    Meteor.subscribe("userList");
Pirog answered 21/6, 2015 at 5:33 Comment(1)
Thanks for the input, but I'm not able to iterate through these in my template. Do you know how that would look?Sumter
L
0

This should work.

  1. subscribe(client)
  2. publish(server)

Client:

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

Server:

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);
});
Lashley answered 21/6, 2015 at 7:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.