2012-11-26 44 views
5

我试图通过电子邮件,使用下面的命令 Meteor.users.findOne({'emails.address': '[email protected]'});流星查询其他用户通过电子邮件

它工作在蒙戈外壳查询用户,但它在流星返回undefined。

任何想法?

UPDATE

原来,我不能查询其他用户。当我查询登录的用户电子邮件时,相同的查询工作。 所以现在的问题我如何查询所有用户?

回答

12

默认情况下,只有流星发表登录的用户,你可以,你提到,运行针对该用户查询。为了访问,你必须把它们发布在服务器上的其他用户:

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 
}); 

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

+1

就是这样。我正在关注当事方的例子,并没有意识到他们发布了这个,但他们称之为'目录'。谢谢! –

4

这可能是有益的:

var text = "[email protected]"; 
Meteor.users.findOne({'emails.address': {$regex:text,$options:'i'}}); 

另见Advance Queries

+1

你的正则表达式不与电子邮件地址的用户,如'鲍勃工作+ 1 @ example.com'。 – Luke

1

首先,您需要发布用户回答上面提到并运行以下命令

Meteor.users.find({"emails": "[email protected]"}).fetch() 

OR

Meteor.users.find({"emails.0": "[email protected]"}).fetch() 
+0

由于Meteor默认使用字段'地址'登录电子邮件,只是添加了以下内容,并且它的工作方式与魅力类似: '''Meteor.users.findOne({“emails.0.address”:“me @ example.com“})'''(在发布所有用户之后,上面提到的安全性声明字段有限,然后订阅)。 –