I have a comment update route that refers to the url /campgrounds/:id/comments/:comment_id
and I use it in the app.js as app.use("/campgrounds/:id/comments", commentsRoutes);
The comments refers to a campground post. This is the Comment Update Route:
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);
}
});
});
I use mongoose to find a comment by url params id, look for the changes in comment and try to update it and redirect to /campgrounds/:id
(the campgrounds show route)
I don't know why, but this code doesn't works: if I log the updatedComment I have:
this is the new comment
{ author: { id: 5a5dc000af7fbb1138a27e33, username: 'patata' },
_id: 5a5e0845f62da603900ab0d5,
text: 'updated text field',
__v: 0 }
the res.redirect
works, but the page shows the old value of the comment, and if I log the campground in the campground show route, I have this output:
{ 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 }
Please note that the comment id is the same (5a5e0845f62da603900ab0d5
). Even if I refresh the page, the comment is always the old one...
This is the Campground show route:
//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});
}
});
});
How can I solve this problem?
Thanks
EDIT: @AngelSalazar this is my campground model:
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);
and this is the Comment model:
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);
So I have to try to find the Campground Id, then find the campground comments and set the new value of the comment from the body req, then save Comment and Campground?
I Try this code:
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);
}
});
}
});
});
The stranger thing is that now the update is done, but the route redirecting to the campground show page works only if the error state is met! The console shows an error, but the campground is updated, I don't now why, but is pretty weird for me...