简体   繁体   English

根据另一个Meteor集合的属性过滤一个Meteor集合

[英]Filtering one Meteor collection based off the property of another Meteor collection

I'm trying to build a site similar to Reddit. 我正在尝试建立一个类似于Reddit的网站。 Here's the link to what I have so far and here's the code for the site. 这是到目前为止我的链接,这是该站点的代码 So if the user creates an account and clicks on one of the website links, it wills send the user to a webpage regarding the website with a comment form underneath it. 因此,如果用户创建一个帐户并单击网站链接之一,它将把用户带到有关该网站的网页,并在其下方带有评论表格。 When the user makes a comment and the clicks Save, their comment will appear at the bottom of the page. 当用户发表评论并单击“保存”时,他们的评论将显示在页面底部。

The problem I'm having is that the comment posted appears on all detailed pages of the websites. 我遇到的问题是发布的评论出现在网站的所有详细页面上。 For example, if the user were to click on the Google link and then post a comment on the page, and then go back and click on Coursera, the comment the user made would appear on the Coursera page as well. 例如,如果用户单击Google链接,然后在页面上发布评论,然后返回并单击Coursera,则用户发表的评论也会显示在Coursera页面上。

Currently I have this code to render the comments into the comments_section template: 目前,我有以下代码将评论呈现到comments_section模板中:

Template.comments_section.helpers({
  comments:function(){
    return Comments.find({});
  }
});

Is there a way I can pass the website id into the find method so that it filters out comments made on other website pages? 有没有一种方法可以将网站ID传递给find方法,以便过滤掉在其他网站页面上发表的评论?

You should chain every comment to that website: 您应该将每个评论链接到该网站:

Create field websiteId 创建字段websiteId

Comments.insert({
  comment:comment,
  postedOn: new Date(),
  websiteId: Router.current().params._id
});

On you route 在您的路线上

this.render('comments_section', {
  to: "section",
  data: function(){
    return Comments.find({websiteId:this.params._id});
  }
});

That should query comment with that website id 那应该查询带有该网站ID的评论

Let me know 让我知道

EDIT 编辑

Instead query in router, try query from template: 而是在路由器中查询,请尝试从模板查询:

Template.comments_section.helpers({
  comments:function(){
    return Comments.find({websiteId:this.params._id});
  }
});

Two options, really. 确实有两个选择。 The first is to what @yudap said above, but the other is to denormalize and add all your comments directly to the post document. 第一个是上面@yudap所说的,另一个是去规范化并将所有评论直接添加到发布文档中。 Eg, your initial insert for a post would look like this: 例如,您的帖子初始插入应如下所示:

Posts.insert({
  title: "this dog is a cat",
  link: "http://nickelodeon.wikia.com/wiki/CatDog",
  comments: []
});

Then whenever someone tries to insert a comment, you can do something like this: 然后,只要有人尝试插入评论,您都可以执行以下操作:

Posts.update(this.params._id, {
  $push: {
    $each: [{
      content: "You're totally right, that dog is a cat",
      userId: Meteor.userId(),
      timestamp: new Date()
    }],
    $sort: { timestamp: 1 }
  }
});

Then you can fetch the post and all the related comments in a single go, if you want to. 然后,您可以单步获取帖子和所有相关评论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM