I am new to Meteor. For learning purpose, I just wanted to list the emails of all users. But can only see email for logged in user.
Notes:
My Template helper looks like:
Template.usersList.helpers({
users () {
return Meteor.users.find({}, {fields: {'username': 1, emails:1}});
},
email(){
if(typeof this.emails === 'undefined'){
console.error(this.emails, "Unauthorized Attempt");
return "--NaN--";
} else {
return this.emails[0].address;
}
}
});
Blaze template:
<template name="usersList">
This is Users List
<ul>
{{#each users}}
<li>{{username}} | {{email}}</li>
{{/each}}
</ul>
</template>
In result, it shows Username for all of the users. And only shows email of logged in user. For other users the "emails" array is returned as undefined.
jessica | [email protected]
Waqas | --NaN--
Bob | --NaN--
In above result jessica is logged in user. Can't see other user's email.
Can someone please tell me how to display emails for all users. Or please point me in the right direction?
Thanks, Ahmad
You can add this on the server:
Meteor.publish("userData", function () {
return Meteor.users.find();
});
And this on the client:
Meteor.subscribe("userData");
For more details, see the documentation.
Mind you, you probably want to limit what you will eventually actually publish to clients. But I think you get the idea. Meteor.users
is just a collection, so you can publish from it just like you would from any other collection.