meteormeteor-accountsmeteor-publications

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


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" : "222@222.com",
                        "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)


Solution

  • Your desperate attempt almost nailed it, try this:

    this.emails.[0].address