meteor.js:通过电子邮件查找用户

时间:2013-10-30 22:59:55

标签: meteor

在我的meteor.js应用程序中,我正在尝试编写一个简单的管理页面,可以通过他/她的电子邮件地址找到用户。

我可以看到在Meteor.users集合中有一个'emails'数组,其中有像这样的对象

{ address : 'foo@foo.com',
  verified : false
}

通常在Mongodb中我可以在这个'电子邮件'数组中搜索,如下所示:

Meteor.users.find({ emails.address : 'foo@foo.com' });

但是这个查询引发了一个错误:

While building the application:
client/admin.js:224:41: Unexpected token .

Aka Meteor不喜欢嵌套查询...

有关如何通过电子邮件地址查询Meteor.users集合的任何想法?

6 个答案:

答案 0 :(得分:58)

您也可以使用您所拥有的内容,只需将其放在引号中:

Meteor.users.find({ "emails.address" : 'foo@foo.com' });

答案 1 :(得分:27)

如果在服务器上,Meteor有一个特殊功能: Accounts.findUserByEmail(email)

我相信这是推荐的方式。

答案 2 :(得分:18)

电子邮件中包含一系列电子邮件。每封电子邮件都有一个地址。

尝试{ emails: { $elemMatch: { address: "foo@foo.com" } } }

有关$elemMatch的信息为here

有关电子邮件作为数组的信息是here

答案 3 :(得分:3)

默认情况下,Meteor只发布登录用户,如您所述,您可以针对该用户运行查询。要访问其他用户,您必须在服务器上发布它们:

Meteor.publish("allUsers", function () {
  return Meteor.users.find({});
});

在客户端订阅他们:

Meteor.subscribe('allUsers');

并运行以下命令

Meteor.users.find({"emails": "me@example.com"}).fetch()

OR

Meteor.users.find({"emails.0": "me@example.com"}).fetch()

Refer this

答案 4 :(得分:3)

如果要在Accounts数组中查找所有电子邮件,并执行不敏感的查询:

const hasUser = Meteor.users.findOne({
    emails: {
      $elemMatch: {
        address: {
          $regex : new RegExp(doc.email, "i")
        }
      }
    }
});

答案 5 :(得分:2)

一种可能的解决方法,如果这在服务器而不是客户端上运行,则在服务器上使用users_by_email方法:

if (Meteor.isServer) {
    Meteor.methods({
        'get_users_by_email': function(email) {
            return Users.find({ emails.address: email }).fetch();
        }
    });
}
if (Meteor.isClient) {
    foo_users = Meteor.call('get_users_by_email', 'foo@bar.baz');
}
相关问题