gpt4 book ai didi

mongodb - 只想显示它所属的产品的评论

转载 作者:可可西里 更新时间:2023-11-01 09:29:52 25 4
gpt4 key购买 nike

我有两个集合,我只想显示被点击产品的评论,但无论我点击什么产品,我都会收到所有评论(来自评论集合)。为了添加和阅读评论,我在 router.js 中有以下代码

      // Add new review
this.route('add_review', {
path:'/add_review/:_id',
template:'add_review',
data: function(){
return Products.findOne(this.params._id)
}
});
// Read reviews
this.route('reviews', {
path:'/reviews/:_id',
template:'reviews',
data: function(){
return Products.findOne(this.params._id)
}
});
});

reviews.js

Template.reviews.helpers({
'reviews': function () {
return Reviews.find( )}
})

reviews.html

<template name="reviews">
<div class="row product-row">
<div class="col-md-2">
<img class="full" src="{{image}}">
</div>
<div class="col-md-10">
<h4>{{name}}</h4>
<p>{{description}}</p>
</div>
</div>
{{#each reviews}}
<p>{{body}} </p>
{{/each}}
</template>

您可以在 GitHub Repository 上找到我项目的完整代码

最佳答案

查看源代码后。您似乎没有在数据库中保存产品和评论之间的任何关联。您将要将产品 _id 存储在评论对象的某个位置。完成后,您将能够在模板中按 productId 过滤评论。我在下面编写了一些示例代码。

add_review.js

Template.add_review.events({
'submit .add_review':function(event){
var rating = event.target.rating.value;
var body = event.target.body.value;
if(body!=""){
Reviews.insert({
rating:rating,
body:body,
productId:Router.current().data()._id //access the product's _id here and save it in this field
});
FlashMessages.sendSuccess('Review Added',{ autoHide: true, hideDelay: 3000 });
Router.go('/');

}
else{
alert('Review field is empty');
}


return false;

}
})

reviews.js

Template.reviews.helpers({
'reviews': function () {
return Reviews.find({productId: Router.current().data()._id}) // again, access the products _id from the router
}
})

关于mongodb - 只想显示它所属的产品的评论,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32770764/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com