使用嵌入式用户数据发布论坛回复

时间:2014-06-17 09:23:26

标签: meteor publish

我正在尝试将论坛回复发布到特定帖子,但我希望这些回复文档包含有关发布该帖子的用户的额外信息。

我不想在回复本身上“保存”额外的信息,而是发布它的“改进”版本。

我在客户端做类似的事情已经使用mycollection.find()。map()并使用map函数在每个返回的文档中嵌入了额外的信息,但是,Meteor发布似乎无法发布数组,只有一个光标,所以简单的地图功能是禁止的。

有没有办法实现这个目标?也许是一个返回光标的“map”函数?

我没有使用Meteor.methods以便我可以有反应性,因为有了它们我可以返回一个数组并正常使用它。

以下是我的代码示例(失败,但发送给出了我需要的内容):

Meteor.publish("forumthread", function(thread){
    return forumReplies.find({thread: thread}).map(function(r){
        // lets fill in additional data about each replies owner
        var owner = Meteor.users.findOne({_id: r.owner});
        if(!owner)
            return; // no owner no reply..
        if(!owner.forumStats){
            owner.forumStats = {};
            owner.forumStats.postCount = 0;
            owner.forumStats.postLikes = 0;
            owner.forumStats.title = "The Newbie";
            owner.forumStats.tag = "Newbie";
            Meteor.users.update({_id: owner._id}, {$set:{ forumStats:owner.forumStats }});
        }
        r.ownerid = owner._id;
        r.ownerUsername = owner.username;
        r.ownerPostCount = owner.forumStats.postCount;
        r.ownerPostLikes = owner.forumStats.postLikes;
        r.ownerTitle = owner.forumStats.title;
        r.ownerTag = owner.forumStats.tag;
        return r;
    });
});

谢谢。

1 个答案:

答案 0 :(得分:0)

结束这样做(发现Christian Fritz也建议这样做):

    Meteor.publish("serverforumthread", function(thread){
    check(thread, String);

    var replies = forumReplies.find({thread: thread});
    var users = {};
    replies.map(function(r){
        users[r.owner] = r.owner;
    });
    var userids = _.map(users, function(value, key){ return value; });   
    var projectedFields = {_id:1, username:1, forumStats: 1, services: 0};    
    var usrs = Meteor.users.find({_id:{$in: userids}}, projectedFields);
    var anyUpdateToUsers = false;
    usrs.map(function(owner){
        var changed = false;
        if(!owner.username){
            owner.username = owner.emails[0].address.split("@")[0]; 
            changed = true;
        }
        //owner.forumStats = undefined;
        if(!owner.forumStats){
            owner.forumStats = {};
            owner.forumStats.postCount = 0;
            owner.forumStats.postLikes = 0;
            owner.forumStats.title = "the newbie";
            owner.forumStats.tag = "newbie";
            owner.forumStats.img = "http://placehold.it/122x122";
            changed = true;
        }
        if(changed){
            anyUpdateToUsers = true;
            Meteor.users.update({_id: owner._id}, {$set:{ forumStats:owner.forumStats }});
        }
    });  
    if(anyUpdateToUsers) // refresh it
         usrs = Meteor.users.find({_id:{$in: userids}}, projectedFields);

    usrs.map(function(owner){
        console.log(owner);
    });
    return [replies, usrs];
});

它适用于以下客户端:

Template.forumReplyOwner.helpers({
    replyOwner: function(reply){
        var owner = Meteor.users.findOne({_id: reply.owner});
        console.log(reply, owner);
        if(!owner || !owner.forumStats) return; // oh shait!
        var r = {};
        r.owner = owner._id;
        r.ownerUsername = owner.username;
        r.ownerPostCount = owner.forumStats.postCount;
        r.ownerPostLikes = owner.forumStats.postLikes;
        r.ownerTitle = owner.forumStats.title;
        r.ownerTag = owner.forumStats.tag;
        r.ownerImg = owner.forumStats.img;
        return r;
    },
    ownerImgTab: function(){
        return {src: this.ownerImg};
    }
});

但是,我现在面临另一个问题。即使我限制我从Users集合发布的字段,它仍然发送“服务”字段,其中包含不应发送的登录数据,想法?