2012-11-26 49 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「})'''(在發佈所有用戶之後,上面提到的安全性聲明字段有限,然後訂閱)。 –