我想在我的网站上发表评论授权。我只想获取我通过ID找到的帖子的评论。并访问该评论的数据。
这是我的帖子路线。
\s
这是我的露营地模型
Campground.findById(req.params.id).populate("comments").exec(function(err, foundCampground){
if(err){
console.log(err);
}else{
User.findById(req.user._id, (err, user)=>{
if(err){
console.log(err);
}else{
Campground.findById({'foundCampground.comments._id': Comments._id}, (err, comments)=>{
console.log("Comments of this post "+comments);
Comments.find({"author.id": user.id}, function(err, comment){
var authID = foundCampground.author.id;
var userID = user.id;
var commentIDs = [];
comment.forEach(function(yorum){
commentIDs.push(yorum.author.id);
});
var commentID = commentIDs[0];
console.log("alinan comments"+comment);
console.log("Comment Id " + commentID);
console.log("UserID "+userID);
if(userID == commentID){
console.log("EQUALO MORUQA");
}
res.render("campgrounds/show",{campground: foundCampground, authID: authID, userID: userID, commentID: commentID});
});
});
}
});
}
});
这是我的评论模型
var campgroundSchema = new mongoose.Schema({
name: String,
image: String,
description: String,
comments: [{
type: mongoose.Schema.Types.ObjectId,
ref: "Comment"
}],
author: {
id: String,
user: String,
}
});
我应该写什么查询才能只获得foundCampground的注释?
答案 0 :(得分:0)
由于您在第一个查询中已经调用了.populate('comments')
,因此可以通过调用foundCampground
foundCampground.comments
的注释。