Meteor.user() doesn't contain emails field


#1

I am using account-google, I don’t know why it doesn’t publish emails to the client.

From the doc about Meteor.user():

By default the server publishes username, emails, and profile (writable by user).

I only get

Object { _id: "vK5ddWtypCewCwbSL", profile: Object }

on my client after running Meteor.user(), but I want to use Meteor.user().emails, anything I missed here?


#2

The email is stored in an array in the profile object.


#3

On a shell within the project directory run: meteor mongo
Then db.users.findOne()
You will then see what’s actually in the database, without having to guess.


#4

If you look at the structure of a Meteor.users document in MongoDB, you will see that it has the following structure:

{ _id: 'SakqFXyKgecDbcLJX',
  createdAt: Sun May 31 2015 11:20:55 GMT+0300 (EAT),
  services: 
   { password: { bcrypt: 'stuff-here' },
     resume: { loginTokens: [Object] },
     email: { verificationTokens: [] } },
  emails: 
   [ { address: 'youremail@example.com',
       verified: true } ],
  profile: 
   { 
       //... and additional stuff here
   }  }

So to get the email of a single user you do the following:

var user = Meteor.users.findOne({}); // or specify the _id of the user
var email = user.emails[0].address;

Notice how the emails attribute is an array and normally if your users create accounts using an email it will be placed in emails[0], the first entry, which in itself is an object so to get the actual email address you look for the address property.


#5

Additional hint: are you sure data is ready (i.e. downloaded from the server) when you call Meteor.user()? At startup time, you might need to wait a bit before accessing the data.


#6

the emails have been persisted in local mongo, I’ve managed to make it working by publish and subscribe it in at the client. Just the doc is pretty confusing.


#7

You need the accounts-password package to get the structure I gave above. Once you’ve done that and if you don’t feel comfortable with the structure of the document; perhaps you might use something like dburles:collection-helpers package to do something like this:

Meteor.users.helpers({
  email: function() {
     return this.emails[0].address;
  }
});

So you can access the email as a method anywhere in your code as follows:

var user = Meteor.findOne({_id: "userIdHere"});
var email = user.email();

Just a thought!


#8

Or maybe it is simple as with Facebook login - there is no email cause you dont need 1 to authenticate.
And if you want email, get it from Facebook API.