如何只使用猫鼬删除用户发布的评论

时间:2020-09-10 02:08:10

标签: node.js express mongoose restapi

enter image description here

Blockquote

        delete( authenticate.verifyUser, (req, res, next) => {
        Dishes.findById(req.params.dishId)
       .then((dish) => {
           if (dish != null && dish.comments.id(req.params.commentId) != null) {

           if(dish.comments.author==req.user.id){

            dish.comments.id(req.params.commentId).remove();
            
        }
  
         dish.save().then((dish) => {
            Dishes.findById(dish._id)
            .populate('comments.author')
            .then((dish) => {
                res.statusCode = 200;
                res.setHeader('Content-Type', 'application/json');
                res.json(dish);  
            })               
        }, (err) => next(err));

当dish.comments.author.id等于req.user.id时,我想删除一条评论。我得到了req.user.id,但是我无法获得dish.comments.author。

enter image description here

0 个答案:

没有答案