2016-07-31 64 views
0

我正在嘗試發佈用戶列表。我正在檢查accoutActive: true的集合,然後獲取studentUserId。我以爲我可以用它來找到meteor.user,但它什麼都不返回。有人能告訴我我錯過了什麼嗎?流星未發佈正確的用戶

Meteor.publish('list', function() { 
    var activeStudent = StudentAccountStatus.find(
          {"accountActive": true}, 
          {fields: 
          {"studentUserId": 1} 
          } 
         ).fetch(); 

    return Meteor.users.find(
        {_id: activeStudent} 
       ); 
}); 
+0

我不明白爲什麼你不直接返回你的第一個查詢。根據需要在發佈函數上返回一個Mongo遊標,移除.fetch()。 –

+0

'activeStudent'被分配了一個對象數組。您需要從中恢復相關字符串(或字符串數​​組並使用'$ in'選擇器)。請注意,結果不會被反應(在額外的「活動」帳戶不會被髮布的意義上),並且非常小心您所使用的用戶字段。 – MasterAM

回答

1

目前您activeStudent變量包含對象這將是這個樣子的數組:

[ { _id: 'a104259adsjf' }, 
    { _id: 'eawor7u98faj' }, 
... ] 

,而你的蒙戈查詢,你只需要一個字符串數組,即['a104259adsjf', 'eawor7u98faj', ...]

所以,你需要通過你的對象數組進行迭代來構造字符串數組,喜歡跟lodash _.map功能:

var activeStudentIds = _.map(activeStudent, function(obj) { 
    return obj._id; 
}); 

然後,使用蒙戈$的選擇,你可以重新制定你的查詢爲:

return Meteor.users.find(
    {_id: { $in: activeStudentIds } } 
);