javascript 在 Meteor JS 应用程序中访问用户电子邮件地址
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14004245/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Access user email address in Meteor JS app
提问by squeezemylime
I am building an app using Meteor and need to access the stored email address of a logged-in user.
我正在使用 Meteor 构建一个应用程序,需要访问已登录用户的存储电子邮件地址。
I am currently using:
我目前正在使用:
var userObj = Meteor.user();
console.log(userObj);
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:
访问用户。但是,我只能访问该 ID。电子邮件地址存储在如下所示的嵌套对象中:
[Object {address="[email protected]", verified=false}]
I have tried various ways to traverse the JSON object but can't figure out how to access the value I need.
我尝试了各种方法来遍历 JSON 对象,但无法弄清楚如何访问我需要的值。
回答by Dave
Meteor.user().emails[0].address
works for me.
Meteor.user().emails[0].address
对我来说有效。
Here's what the doc says:
这是文档所说的:
By default the server publishes username, emails, and profile. See Meteor.usersfor more on the fields used in user documents.
默认情况下,服务器发布用户名、电子邮件和配置文件。有关用户文档中使用的字段的更多信息,请参阅 Meteor.users。
Example user document:
示例用户文档:
{
_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: "[email protected]", verified: true },
{ address: "[email protected]", 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 }
]
}
}
}
回答by David Wihl
You don't specify how you are authenticating users. For example, if you were using Google authentication only, the email address would be found only in
您没有指定如何对用户进行身份验证。例如,如果您仅使用 Google 身份验证,则只能在以下位置找到电子邮件地址
Meteor.user().services.google.email
So, it depends.
所以,这取决于。
回答by Ruben
Try this:
试试这个:
Meteor.user().emails[0].address
Meteor.user().emails[0].address
Regards,
问候,