我正在使用 Meteor 构建一个应用程序,需要访问已登录用户存储的电子邮件地址。
我目前正在使用:
var userObj = Meteor.user();
console.log(userObj);
访问用户。但是,我只能访问 id。电子邮件地址存储在一个嵌套对象中,如下所示:
[Object {address="address@gmail.com", verified=false}]
我尝试了各种方法来遍历 JSON 对象,但不知道如何访问我需要的值。
最佳答案
Meteor.user().emails[0].address
对我有用。
这是文档所说的:
By default the server publishes username, emails, and profile. See Meteor.users for more on the fields used in user documents.
示例用户文档:
{
_id: "bbca5d6a-2156-41c4-89da-0329e8c99a4f", // Meteor.userId()
username: "cool_kid_13", // unique name
emails: [
// each email address can only belong to one user.
{ address: "cool@example.com", verified: true },
{ address: "another@different.com", verified: false }
],
createdAt: 1349761684042,
profile: {
// The profile is writable by the user by default.
name: "Joe Schmoe"
},
services: {
facebook: {
id: "709050", // facebook id
accessToken: "AAACCgdX7G2...AbV9AZDZD"
},
resume: {
loginTokens: [
{ token: "97e8c205-c7e4-47c9-9bea-8e2ccc0694cd",
when: 1349761684048 }
]
}
}
}
关于javascript - 在 Meteor JS 应用程序中访问用户电子邮件地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14004245/