2016-11-07 27 views
2

我有一個流星發佈函數(在服務器端代碼中),它返回兩個不同集合中的兩個光標的數組。 在每種情況下,返回的遊標都應包含滿足某些查詢條件的文檔,這些查詢條件作爲Meteor.publish中的函數的參數提供。 下面的代碼會更清楚:使用流星的參數進行訂閱

//server 
Meteor.publish('publisher', function(userId){ 
return[ 
    posts.find({createdBy: userId}), 
    accounts.find({_id: userId}) 
    ]; 
}); 
//client 
Meteor.subscribe('publisher',Session.get('userId')); 
//this code runs within a meteor method on the client 
var id = Session.get('userId'); 
console.log(id) 
var acnts = accounts.find({_id: id}).fetch(); 
console.log(acnts); 

有一個登錄按鈕,設置所謂的「用戶id」的會話。 控制檯記錄當前的id,但記錄到控制檯的文檔始終爲空(雖然它存在)。

任何幫助將不勝感激。 :)

+1

由於代碼不在上下文中,所以稍微難以分辨發生了什麼,但這通常是因爲訂閱還沒有準備好。如果你在瀏覽器中用'accounts.find()'在行上放置一個斷點,然後讓它在暫停後再次運行,現在它可以工作,那麼這就是你的問題。 –

回答

0

將您的客戶端代碼取決於回調中的訂閱。

//server 
Meteor.publish('publisher', function(userId){ 
return[ 
    posts.find({createdBy: userId}), 
    accounts.find({_id: userId}) 
    ]; 
}); 

//client 
Meteor.subscribe('publisher',Session.get('userId'),function(){ 
    //this code runs within a meteor method on the client 
    var id = Session.get('userId'); 
    console.log(id) 
    var acnts = accounts.find({_id: id}).fetch(); 
    console.log(acnts); 

});