2016-03-24 32 views
0

我有一个蒙戈文件格式如下:链接用户可以通过ID流星发布

Group: { 
    participants: [ 
     userId, 
     userId, 
     userId 
    ] 
} 

...其中的userIds明显流星自己的用户文档的ObjectID。

我真的有这个问题,我希望用户在他们的组中查看其他用户信息。在这个实现中,我想象一个安全的(阅读:我删除了自动发布和不安全的)组邮件系统。

我目前的发布实施是这样的:

//grab all groups user belongs to 
Meteor.publish("groups", function() { 
    var groups = Groups.find({ 
     participants: { 
      $in: [ this.userId ] 
     } 
    }); 
    return groups; 
}); 

现在,理想情况下,我很想只是执行一些代码来操纵groups之前,我在完成发布它也公布每个参与者的user.profile数据也是如此。想象将作为最终的格式如下:

Group: { 
    participants: { 
     userId 
    }, 
    users: { 
     { //One of these for each user 
      userId, 
      firstName, 
      lastName, 
      otherData 
     } 
    } 
} 

有一件事我注意到的是,没有和自动发布不安全,我不能只通过一个辅助函数做这个客户端上。

回答

1

这是一个相当简单的用例的reywood:publish-composite包:

Meteor.publishComposite('groups', { 
    find: function() { 
     return Groups.find({ participants: { $in: this.userId }}); 
    }, 
    children: [ 
     { 
      find: function(group) { 
       return Meteor.users.find(
        { _id: { $in: group.participants }, 
        { fields: { firstName: 1, lastName: 1, otherData: 1 }}); 
      } 
     }, 
    ] 
}); 

注意的是,用户的_id字段总是包含在内,您无需显式调用它在fields:列表。

+0

从这里我如何访问该子数据?我正在将这个集合记录到Chrome的控制台,我没有看到任何这样的新阵列。 – Henry

+0

像,代码正在构建,但我没有看到任何新的数据发送到客户端。 – Henry

+0

在客户端上,您将看到Meteor.users集合中的其他文档,但仅包含您请求的字段。没有单独的阵列。 –