我的评论更新路线引用了网址/campgrounds/:id/comments/:comment_id
,我在app.js中将其用作app.use("/campgrounds/:id/comments", commentsRoutes);
评论指的是露营地的帖子。这是评论更新路线:
router.put("/:comment_id", function (req, res) {
Comment.findByIdAndUpdate(req.params.comment_id, req.body.comment, {new: true})
.exec(function(err, updatedComment){
if (err) {
res.redirect("back");
} else {
console.log("this is the updated comment" + updatedComment);
res.redirect("/campgrounds/" + req.params.id);
}
});
});
我使用mongoose通过url params id查找评论,查找评论中的更改并尝试更新并重定向到/campgrounds/:id
(露营地显示路线)
我不知道为什么,但这段代码不起作用:如果我记录了更新后的评论:
this is the new comment
{ author: { id: 5a5dc000af7fbb1138a27e33, username: 'patata' },
_id: 5a5e0845f62da603900ab0d5,
text: 'updated text field',
__v: 0 }
res.redirect
有效,但页面显示评论的旧值,如果我在露营地展示路线中记录露营地,我有这个输出:
{ author: { id: 5a5dc000af7fbb1138a27e33, username: 'patata' },
comments:
[ { author: [Object],
_id: 5a5e0845f62da603900ab0d5,
text: 'old text field',
__v: 0 } ],
_id: 5a5e083af62da603900ab0d4,
name: 'Armando',
image: 'https://i.pinimg.com/originals/2e/b7/e2/2eb7e206036f11c94ed0cac8fb1fc05e.jpg',
description: 'lalala',
__v: 1 }
请注意,评论ID是相同的(5a5e0845f62da603900ab0d5
)。即使我刷新页面,评论也总是旧的......
这是营地显示路线:
//SHOW
router.get('/:id', function (req, res) {
//find correct campground ID
Campground.findById(req.params.id).populate("comments").exec(function (err, foundCampground) {
if (err) {
console.log(err);
} else {
// render the show template
console.log(foundCampground);
res.render('campgrounds/show', {campground:foundCampground});
}
});
});
我该如何解决这个问题?
由于
编辑:@AngelSalazar这是我的露营地模型:var campgroundSchema = new mongoose.Schema({
name: String,
image: String,
description: String,
author: {
id: {
type: mongoose.Schema.Types.ObjectId,
ref: "User"
},
username: String
},
comments: [{
type: mongoose.Schema.Types.ObjectId,
ref: "Comment"
}]
});
module.exports = mongoose.model("Campground", campgroundSchema);
这是评论模型:
var mongoose = require("mongoose");
var commentSchema = new mongoose.Schema({
text: String,
author: {
id: {
type: mongoose.Schema.Types.ObjectId,
ref: "User"
},
username: String
}
});
module.exports = mongoose.model("Comment", commentSchema);
所以我必须尝试找到Campground Id,然后找到露营地评论并从body req设置评论的新值,然后保存评论和营地?
我试试这段代码:
router.put("/:comment_id", function (req, res) {
Comment.findByIdAndUpdate(req.params.comment_id,
{$set: {text: req.body.comment.text}},
{new: true},
function (err, updatedComment) {
if (err) {
res.redirect("back");
console.log("error");
} else {
Campground.findByIdAndUpdate(req.params.id,
{$set: {"comments.text": updatedComment.text}},
{new: true},
function (err, foundCampground) {
if (err) {
res.redirect("/campgrounds/" + req.params.id); // redirect to show page correctly
console.log("error"); // log error
console.log(foundCampground); // log undefined
} else {
res.redirect("/campgrounds/" + req.params.id);
console.log("ok");
console.log(foundCampground);
}
});
}
});
});
更奇怪的是,现在更新已完成,但重定向到露营地展示页面的路线只有在满足错误状态时才能 !控制台显示错误,但露营地已更新,我现在不知道为什么,但对我来说非常奇怪......
答案 0 :(得分:0)
您必须使用$set
来定义要更新的内容。
请检查文档页面上的mongoose文档,更新项目: http://mongoosejs.com/docs/documents.html
答案 1 :(得分:0)
评论放置路线
// Update for "mongoose": "^4.11.9",
router.put("/:comment_id", function(req, res){
Comment.findByIdAndUpdate(req.params.comment_id, req.body.comment, function(err, comment){
if(err){
console.log(err);
res.render("edit");
} else {
res.redirect("/campgrounds/" + req.params.id);
}
});
});
但是,请检查此页面中的MongoDB兼容性:http://mongoosejs.com/docs/compatibility.html,因为您可能需要使用usePushEach
更改模型:
评论模型
var mongoose = require("mongoose");
var commentSchema = new mongoose.Schema({
text: String,
author: {
id: {
type: mongoose.Schema.Types.ObjectId,
ref: "User"
},
username: String
}
}, { usePushEach: true });
module.exports = mongoose.model("Comment", commentSchema);
希望它有所帮助。再见