17

我正在使用 Meteor 构建一个应用程序,并且需要访问已登录用户的存储电子邮件地址。

我目前正在使用:

var userObj = Meteor.user();
console.log(userObj);

访问用户。但是,我只能访问 id。电子邮件地址存储在一个嵌套对象中,如下所示:

[Object {address="address@gmail.com", verified=false}]

我尝试了各种方法来遍历 JSON 对象,但无法弄清楚如何访问我需要的值。

4

3 回答 3

35

Meteor.user().emails[0].address为我工作。

这是文档所说的:

默认情况下,服务器会发布用户名、电子邮件和个人资料。有关用户文档中使用的字段的更多信息,请参阅 Meteor.users

示例用户文档:

{
  _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 }
      ]
    }
  }
}
于 2012-12-22T17:35:08.997 回答
5

您没有指定如何对用户进行身份验证。例如,如果您仅使用 Google 身份验证,则电子邮件地址只能在

Meteor.user().services.google.email

所以,这取决于。

于 2012-12-23T03:34:36.863 回答
3

试试这个:

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

问候,

于 2016-02-24T00:05:35.300 回答