I am building an app using Meteor and need to access the stored email address of a logged-in user.
I am currently using:
JavaScript
x
3
1
var userObj = Meteor.user();
2
console.log(userObj);
3
to access the user. However, I am only able to access the id. The email address is stored in a nested object that looks like this:
JavaScript
1
2
1
[Object {address="address@gmail.com", verified=false}]
2
I have tried various ways to traverse the JSON object but can’t figure out how to access the value I need.
Advertisement
Answer
Meteor.user().emails[0].address
works for me.
Here’s what the doc says:
By default the server publishes username, emails, and profile. See Meteor.users for more on the fields used in user documents.
Example user document:
JavaScript
1
27
27
1
{
2
_id: "bbca5d6a-2156-41c4-89da-0329e8c99a4f", // Meteor.userId()
3
username: "cool_kid_13", // unique name
4
emails: [
5
// each email address can only belong to one user.
6
{ address: "cool@example.com", verified: true },
7
{ address: "another@different.com", verified: false }
8
],
9
createdAt: 1349761684042,
10
profile: {
11
// The profile is writable by the user by default.
12
name: "Joe Schmoe"
13
},
14
services: {
15
facebook: {
16
id: "709050", // facebook id
17
accessToken: "AAACCgdX7G2...AbV9AZDZD"
18
},
19
resume: {
20
loginTokens: [
21
{ token: "97e8c205-c7e4-47c9-9bea-8e2ccc0694cd",
22
when: 1349761684048 }
23
]
24
}
25
}
26
}
27