2016-07-31 82 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 } } 
);