如何在Meteor中与Mongo集合相关联而不使用简单模式?

3

我正在开发一个Meteor项目。

步骤1:

我已经添加了accounts-password和accounts-ui包,以便拥有用户集合和认证系统。

步骤2:

我创建了一个Mongo集合'Posts',包含以下字段:_id、title、description和createdOn(日期)。

步骤3:

我创建了另一个Mongo集合'Comments',包含以下字段:_id、comment、postedOn(日期)和createdBy(Meteor.user()._id)。

步骤4:

我添加了iron router包并设置了一些路由。您可以查看博客列表并转到单个帖子详细页面。 我想为已登录的用户提供在单个评论上发布评论的可能性,而不使用aldeed simple-schema包。

下面是我的项目中的一些片段:

 Template.posts_list.helpers({
    posts:function(){
        return Posts.find({}, {sort: {createdOn: -1} });
    }
})
 Template.comments.helpers({
    comments:function(){
        return Comments.find({ ?????  Ho can I associate comments to a single post? });

    }
})

我想知道如何正确地将这两个集合关联起来。我希望只显示与相关文章关联的评论。目前,所有评论似乎都出现在每篇文章中,没有区别。有什么帮助吗?谢谢。
1个回答

0
你想在评论模式中添加一个postId。然后,每当您提交评论时,获取相关帖子的_id并将其发送到您插入评论的Meteor方法中。类似于这样:
// In your template events:
'submitCommentForm': function( event, template ) {
    var postId = this._id; // Make sure your post data context of this form is set in a #each or #with.
    Meteor.call('addComment', event.target.comment, postId, ...) // Assuming your comment is in some sort of named input with comment as the name.
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接