在Meteor发布中通过id链接用户

时间:2016-03-24 17:40:40

标签: javascript mongodb meteor publish meteor-publications

我有一个mongo文档,格式如下:

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

...其中userIds显然是Meteor自己的用户doc的ObjectIds。

我真正遇到的问题是我希望用户在其群组中查看其他用户信息。在这个实现中,我想象一个安全的(读取:我删除了自动发布和不安全)组消息系统。

我目前的发布实现如下:

//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 个答案:

答案 0 :(得分: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:列表中明确地将其标注出来。