Reputation: 364
I am attempting to edit an array within an object on my Express/NodeJS application. Below is the data from the collection of Locations:
{ _id"56873fc9182b3741059357d0",
longitude: 113.83507800000007,
latitude: 22.1533884,
location: "Hong Kong",
name"Hong Kong",
__v0,
reviews: {review_id: "OBY3iC1IcdIE", comment: null, rating: null }
{review_id: "3433iC1IcdIY", comment: null, rating: null }
}
I'd like to edit one of the reviews (example: review_id:"OBY3iC1IcdIE") but my server side code makes the app crash. My current server side code is:
exports.editReview = function(req, res) {
Location.update({ _id: req.params.location_id, reviews.review_id: req.params.review_id },
{ $set: { review.comment: req.body.comment,
review.rating: req.body.rating }}, function(err, location) {
if(err)
res.send(err);
res.json(location);
});
};
Upvotes: 0
Views: 994
Reputation: 364
I have to say thank you @SumanLama for the assistance. I added the necessary $ signs as per the MongoDB documentation.
exports.editReview = function(req, res) {
Location.update({ _id: req.params.location_id, "reviews.review_id": req.params.review_id },
{ $set: { "reviews.$.comment": req.body.comment,
"reviews.$.rating": req.body.rating }}, function(err, location) {
if(err)
res.send(err);
res.json(location);
});
};
Upvotes: 0
Reputation: 946
exports.editReview = function(req, res) {
Location.update({ _id: req.params.location_id, "reviews.review_id": req.params.review_id },
{ $set: { "reviews.comment": req.body.comment,
"reviews.rating": req.body.rating }}, function(err, location) {
if(err)
res.send(err);
res.json(location);
});
};
Try this, add double quotes when you need to use nested fields. I am assuming your mongodb setup is fine in your app
Upvotes: 2