【问题标题】:Meteor Querying other users by emailMeteor 通过邮件查询其他用户
【发布时间】:2015-11-08 04:23:13
【问题描述】:

我正在尝试使用以下命令通过电子邮件查询用户 Meteor.users.findOne({'emails.address': 'me@example.com'});

它在 mongo shell 中工作,但在 Meteor 中返回 undefined。

有什么想法吗?

更新

原来我无法查询其他用户。当我查询登录的用户电子邮件时,相同的查询有效。 那么现在的问题是如何查询所有用户?

【问题讨论】:

    标签: mongodb meteor


    【解决方案1】:

    默认情况下,Meteor 只发布登录用户,正如你提到的,你可以对该用户运行查询。为了访问其他用户,您必须在服务器上发布他们:

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

    并在客户端订阅它们:

    Meteor.subscribe('allUsers');
    

    另外请记住,您可能不想发布所有字段,因此您可以指定要发布/不发布的字段:

    return Meteor.users.find({}, 
    {
         // specific fields to return
         'profile.email': 1,
         'profile.name': 1,
         'profile.createdAt': 1
    });
    

    发布集合后,您可以为所有用户运行查询和访问信息。

    【讨论】:

    • 就是这样。我在关注当事人的例子,并没有意识到他们发布了这个,但他们称之为“目录”。谢谢!
    • 所以订阅后,我可以以Meteor.users.find({'profile.isAdmin':true}).toArray();的身份访问该集合
    【解决方案2】:

    这可能会有所帮助:

     var text = "me@example.com";
     Meteor.users.findOne({'emails.address': {$regex:text,$options:'i'}});
    

    另见Advance Queries

    【讨论】:

    • 您的正则表达式不适用于电子邮件地址为bob+1@example.com 的用户。
    【解决方案3】:

    首先你需要发布上面提到的用户答案并运行以下命令

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

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

    【讨论】:

    • 由于 Meteor 默认将登录电子邮件放入带有字段“地址”的数组中,只需添加以下内容,它就像魅力一样工作:'''Meteor.users.findOne({"emails.0. address": "me@example.com"})''' (在发布了所有用户之后,上面提到的安全声明字段有限,然后订阅)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-20
    • 1970-01-01
    • 1970-01-01
    • 2011-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多