我正在尝试建立一个类似于 Reddit 的网站。这是我拥有的链接 so far这是 code对于该网站。因此,如果用户创建一个帐户并单击其中一个网站链接,它会将用户发送到有关该网站的网页,其下方有一个评论表单。当用户发表评论并单击“保存”时,他们的评论将显示在页面底部。
我遇到的问题是发布的评论出现在网站的所有详细页面上。例如,如果用户单击 Google 链接,然后在页面上发表评论,然后返回并单击 Coursera,则用户所做的评论也会显示在 Coursera 页面上。
目前我有以下代码将评论呈现到 comments_section 模板中:
Template.comments_section.helpers({
comments:function(){
return Comments.find({});
}
});
有没有办法可以将网站 ID 传递到 find 方法中,以便过滤掉其他网站页面上的评论?
最佳答案
您应该将每条评论链接到该网站:
创建字段websiteId
Comments.insert({
comment:comment,
postedOn: new Date(),
websiteId: Router.current().params._id
});
在您的路线上
this.render('comments_section', {
to: "section",
data: function(){
return Comments.find({websiteId:this.params._id});
}
});
应该使用该网站 ID 查询评论
让我知道
编辑
尝试从模板查询,而不是在路由器中查询:
Template.comments_section.helpers({
comments:function(){
return Comments.find({websiteId:this.params._id});
}
});
关于javascript - 根据另一个 Meteor 集合的属性过滤一个 Meteor 集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35453973/