2016-01-22 66 views
2

我目前正在做一個簡單的流星應用程序,它需要顯示除當前登錄用戶以外的所有用戶。流星顯示當前登錄用戶以外的所有用戶

這裏是我的「用戶」模板,顯示所有用戶:

<template name="friends"> 
    {{#each listUser}} 
     <p id="userNameOnList">{{profile.firstname}} {{profile.lastname}} <a href="#" class="btn btn-primary btnAddFriend">Add Friend</a></p> 
    {{/each}} 
</template> 

這是我的模板助手:

Template.friends.helpers({ 
    listUser: function(){ 
     return Meteor.users.find({},{sort:{'profile.firstname': 1}}); 
    } 
}); 

我有點自丟在這裏,你能上給出出主意我如何處理這個問題?謝謝!

回答

2

在查詢中添加您當前的userId。我沒有測試查詢,但它會工作

Template.friends.helpers({ 
    listUser: function(){ 
     return Meteor.users.find({_id:{$ne:Meteor.userId()}},{sort:{'profile.firstname': 1}}); 
    } 
}); 
+1

它的工作,謝謝你!我一直在堅持。 – ickyrr

1

docs是你的朋友在這裏。因爲它明確規定

像所有Mongo.Collections,您可以訪問 服務器上的所有文件,但只有具體由服務器發佈這些都是 在客戶端上可用。

默認情況下,當前用戶的用戶名,電子郵件和配置文件是發佈給客戶端的 。您可以發佈更多的字段與 當前用戶:

// server 
Meteor.publish("userData", function() { 
    if (this.userId) { 
    return Meteor.users.find({_id: this.userId}, 
          {fields: {'other': 1, 'things': 1}}); 
    } else { 
    this.ready(); 
    } 
}); 

// client 
Meteor.subscribe("userData"); 

如果安裝包自動發佈,所有用戶 系統上的信息被髮布到所有客戶端。這包括用戶名, 個人資料以及任何旨在公開的服務字段(例如 services.facebook.id,services.twitter.screenName)。此外,當 使用自動發佈更多信息發佈當前 登錄用戶,包括訪問令牌。這允許直接從客戶端調用API調用 以獲得允許的服務。


大廈從上面的,你可以定製你自己的發佈功能。您首先需要從Mongo.users集合中獲取用戶登錄的_id。然後用你的查詢返回所有 用戶減去當前登錄的用戶:

// in server.js 
Meteor.publish("userData", function() { 
    return Meteor.users.find(
     {"_id": { "$ne": this.userId }}, 
     {fields: {emails: 1, profile: 1}} 
    ); 
}); 

// in client.js 
Meteor.subscribe("userData"); 
+1

爲了補充一點,使用'null'的訂閱將自動發佈。因此,使用'Meteor.publish(null,function(){/ ** ... * /})'更容易發佈關於當前用戶的其他信息 – corvid

相關問題