How to access Meteor.users() fields from the client?

341 Views Asked by At

I want to access users emails in from the client, here is what I have done, in the server side:

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

In the client side:

Template.usersManagement.onCreated(function () {
    var self = this;
    self.autorun(function() {
        self.subscribe('userList');
    });
});

And the template helper to retrieve the users:

Template.usersManagement.helpers({
    allUsers: function() {
        console.log(Meteor.users.find({}).fetch())
        return Meteor.users.find({}).fetch()
    }
})

In the usersManagement template:

{{#each allUsers}}

    <h1>{{this.emails.address}}</h1>
    <h1>{{this.profile.name}}</h1>
                    
{{/each}}

The users names are displayed but the email isn't and no errors showing in the console.

Here is the look on how the users stored in the database:

{
        "_id" : "m7admvZc32Jr3SeiE",
        "createdAt" : ISODate("2017-12-27T21:24:48.927Z"),
        "services" : {
                "password" : {
                        "bcrypt" : "$2a$10$wv6KsRp6s91A.0mHH89Q0eT3jrZmJjKJhw8SIH9c8c8OpwMrXyGMC"
                }
        },
        "emails" : [
                {
                        "address" : "[email protected]",
                        "verified" : false
                }
        ],
        "profile" : {
                "name" : "222",
                "createdAt" : ISODate("2017-12-27T21:24:48.707Z"),
                "updatedAt" : ISODate("2017-12-27T21:24:48.707Z"),
                "group" : "admin"
        },
        "status" : {
                "online" : false
        }
}

My question is, how can I retrieve the emails of the users?

EDIT

Here is how the emails are retrieved (from the console):

emails

There is another field (0) below the emails. I tried this.emails.0.address it didn't work (desperate attempt)

2

There are 2 best solutions below

2
Behrouz Riahi On BEST ANSWER

Your desperate attempt almost nailed it, try this:

this.emails.[0].address
0
U Rogel On

An item in the Meteor.users collection store emails as an array, for you to be able to store more than one email-address per user. So since it is so, the right way would be:

<h1>{{this.emails[0].address}}</h1>