Home > other >  How to edit comment that is inside of Post schema?
How to edit comment that is inside of Post schema?

Time:12-12

I'm working on app where user can make post and comment. I'm trying to edit comment that is inside of a post. I work with MERN (mongoose, express, react, nodejs). I can successfully delete comment, but don't know how to edit its comment. This is Post Schema

const mongoose = require('mongoose')
const { ObjectId } = mongoose.Schema.Types
const postSchema = new mongoose.Schema({

    title: {
        type: String,
        required: true
    },
    body: {
        type: String,
        required: true
    },
    photo: [{
        type: String,
        required: true
    }],
    likes: [{ type: ObjectId, ref: "User" }],
    comments: [{
        text: String,
        postedBy: { type: ObjectId, ref: "User" }
    }],
    postedBy: {
        type: ObjectId,
        ref: "User"
    }
}, { timestamps: true })

mongoose.model("Post", postSchema)

This is my Post Schema I want to edit comment.

Thos is my Delete

router.delete('/deletecomment/:postId/:commentId', requireLogin, (req, res) => {
    Post.findOne({ _id: req.params.postId })
        .populate("postedBy", "_id")
        .populate('comments.postedBy', '_id name')
        .exec((err, post) => {
            if (err || !post) {
                return res.json({ error: err });
            }
            post.comments = post.comments.filter(item => {
                if (!(item.postedBy._id.toString() === req.user._id.toString() && item._id.toString() === req.params.commentId.toString())) {
                    return item
                }
            })
            post.save()
                .then(result => {
                    Post.findOne({ _id: result.id })
                        .populate('postedBy', '_id name profilePhoto')
                        .populate('comments.postedBy', '_id name')
                        .exec((err, post) => {
                            if (err || !post) {
                                return res.json({ error: err });
                            }
                            res.json({ result: post, message: 'Successfully deleted comment!' });
                        })
                })
                .catch(err => {
                    console.error(err);
                })

        })
});

This what I have tried but dont know how to edit my comment/

router.put('/editcomment/:postId/:commentId', requireLogin, (req, res) => {
    const editComment = {
        text: req.body.text,
        postedBy: req.user._id
    }
    Post.findByIdAndUpdate(req.body.postId,
        {
            $set: { comments: editComment }
        }, {
        new: true
    })

        .populate("comments.postedBy", "_id name")
        .populate("postedBy", "_id name")
        .exec((err, result) => {
            console.log("result => ", result)
            if (err) {
                return res.status(422).json({ error: err })
            } else {
                res.json(result)
            }

        })
})

CodePudding user response:

    Post.updateOne({
           _id: ObjectId()
        },
        {
            $set: { comments:{ text: 'editComment' }}
        }
    })
.then((resolve) => {
console.log(resolve)
})

You only have to put the right query to find the document .It will work

CodePudding user response:

try to replace req.body.postId to req.body.commentId

or try this

  router.put("/editcomment/:postId/:id", requireLogin, (req, res) => {
    try {
      const id = req.params.id;
      const options = { new: true };

      const editComment = {
        text: req.body.text,
        postedBy: req.user._id,
      };
      Post.findByIdAndUpdate(id, editComment, options);
      return res.status(200).json({ result: editComment });
    } catch (err) {
      return res.status(422).json({ error: err });
    }
  });
  • Related