0

This is my schema:

    let userSchema = new mongoose.Schema({
    id: String,
    displayName: String,
    displayImage: String,
    posts: [
        {
            url: String,
            description: String,
            likes: [String],
            comments: [
                { content: String, date: String, author: { id: String, displayName: String, displayImage: String } }
            ]
        }
    ]
});

I am trying to edit the elements in the comment array and am already aware that I should have made two separate schemas due to MongoDB's limited functionality when it comes to manipulating double nested docs.

But anyhow, this seems to work for me, remember I am trying to edit the contents of a particular comment in the comment array.

controller.editComment = (req, res, next) => {
User.findOne(
    { id: req.query.userid, 'posts._id': req.params.postid },
    { 'posts.$.comments._id': req.body.commentID }
)
    .exec()
    .then((doc) => {
        let thisComment = doc.posts[0].comments.filter((comment) => { return comment._id == req.body.commentID; });
        thisComment[0].content = req.body.edited;
        doc.save((err) => { if (err) throw err; });
        res.send('edited');
    })
    .catch(next);
};

This works, however it ALWAYS only updates the comments of the very first post, regardless of which comment I edit. But please keep in mind that thisComment[0].content, if console.logged, will always show the correct content of the correct comment under the correct post. However, upon doc.save(err) is where I assume the problem is happening.

Any direction is GREATLY appreciated, I really can't see what seems to be the problem.

JohnSnow
  • 6,911
  • 8
  • 23
  • 44

0 Answers0