2014-07-12 67 views
0

我仍然在努力瞭解如何從另一個集合查詢中作爲外鍵訪問Meteor.users。據我所知,只有當前用戶默認情況下發布,所以我必須在服務器上發佈的流星用戶集合和Deps.autorun問題

Meteor.publish('itemOwner', function(userId) { 
    check(userId, String); 
    var user = Meteor.users.find({id: userId}); 
    return user; 
    // return Meteor.users.find({id: userId}, { 
    // fields: {'profile': 1} }); 
}); 

然後我有一個Deps.autorun在客戶端上..

Deps.autorun(function() { 
    var itemOwnerId = Session.get("itemOwnerID"); 
    if (itemOwnerId) { 
    debugger 
    var ID = Session.get("itemOwnerID"); 
    Meteor.subscribe('itemOwner', Session.get("itemOwnerID")); 
    } 
}); 

我設置會話ID的模式窗體負載,並通過調用ownerProfile助手在模板顯示它(或嘗試)現在

Template.showQuoteModalInner.helpers({ 
    getQuote: function() { 
    // Get the quote ID from the session var 
    var quote = Session.get("quoteID"); 
    if(quote) { 
     debugger; 
     var ID = quote.user._id; 
     Session.set("itemOwnerID", quote.user._id); 
     return quote; 
    } 
    }, 
    ownerProfile: function() { 
    debugger; 
    var quote = Session.get("quoteID"); 
    if(quote) { 
     var ID = quote.user._id; 
     var theUser = Meteor.users.find({_id: quote.user._id}); 
     return theUser; 
    }; 
    } 
}); 

,我可以在每個階段跟蹤用戶ID,並看到它得到正確地傳遞給自動運行一個d幫助者。如果我在ownerProfile助手的調試器中停止程序,並在控制檯中放入Meteor.user.fetch({_ id:「id here here」})。fetch()我得到正確的用戶回來..但是,在處理程序本身的Meteor.users.find返回空???我錯過了什麼?

回答

2

我注意到了兩種可能性。

首先,您在發佈功能中找不到下劃線。

.find({id: userId})應該是.find({_id: userId})

但是,如果您在控制檯中看到用戶(登錄用戶除外),則這可能不是問題。其次,如果您沒有看到Template.showQuoteModalInner.ownerProfile幫助程序中的用戶,可能是因爲您正在返回一個find()而不是findOne()。

find()返回一個遊標,而findOne()返回記錄。如果你想顯示那個用戶的屬性,試試findOne()。

+0

修復了這兩個問題及其全部工作!謝謝。一直看着這個日子,但沒有看到。 –