Cant't print the User email

Uncaught TypeError: Cannot read property ‘emails’ of undefined

when I am using Meteor.user().emails[0].address my Approach following

  1. const currentUserEmail = Meteor.user.emails[0].address;

console.log(currentUSerEmail)

const email = user.emails[0].address;
console.log(email);```

I’m Using Meteor 1.9 With React 16.12

I’m Using the same thing In Browser Console, here is the snap of it

Its Nit working anywhere and i also disable autopublish and insecure but nothing change

Thank You In Advance … Peace

should it be

Meteor.user().emails[0].address
1 Like

Yes But it’s Not Working with react.

ohh ok, just checking :slight_smile:

See Here is my file.

If you actually have a Meteor.user() you must be having a problem of optional chaining. Please read here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining

If you are set with Babel for ES2020 you should be able to write that as
Meteor.user()?.emails?.[0]?.address
which is equal to
Meteor.user() && Meteor.user().emails && Meteor.user().emails[0] && Meteor.user().emails[0].address
You can also write it like:

const currentUserEmail = Meteor.user() ? Meteor.user().emails[0].address : ''

If you use Social Logins, you don’t have a emails object key in your user. What we normally do is to write the email on the Profile or somewhere so that regardless of the source of login, you avail of the email at the same location. Perhaps this is not your case.
Could you console.log(Meteor.user()) just to make sure that you can actually see the emails.

1 Like

This looks like you are trying to access data before it’s been sent to the client. Meteor.user() relies on the user publication, and it can take a second after page load for the data to be sent to the client.

You should wrap that component wrapped in a withTracker and pass the user in as a prop, that way it will update when it receives the user data