Mongoose无法更新我的文档

时间:2022-06-02 17:12:36

I need to batch delete the feedbacks to article, comment or reply entity. Here is my implementation

我需要批量删除对文章、评论或回复实体的反馈。这是我的实现

exports.batchDeleteFeedback = function(req, res, next) {
    var articleId = req.body.articleId
    var commentId = req.body.commentId
    var replyId = req.body.replyId

    var Target, targetId, field
    if (articleId) {
        Target = Article
        targetId = articleId
        field = 'article'
    }
    else if (commentId) {
        Target = Comment
        targetId = commentId
        field = 'comment'
    }
    else if (replyId) {
        Target = Reply
        targetId = replyId
        field = 'reply'
    }


    var criteria = {}
    criteria['target.' + field] = targetId

    Feedback.update(criteria, {$set: {
        'status': 'deleted'
        ,'status.deletedBy': req.user
        ,'status.deletedDate': new Date
    }}, {multi: true}, function(err) {
        if (err)
            return next(err)

        Target.findById(targetId).exec(function(err, target) {
            if (err)
                return next(err)

            var json = target.toJSON()
            json.success = 'success batch delete feedbacks'
            return res.json(json)
        })
    })
}

And I got this error message

我得到了这个错误信息

cannot use the part (status of status.deletedBy) to traverse the element ({status: "deleted"})

What is the problem with my code?

我的代码有什么问题?

1 个解决方案

#1


3  

You're treating the status property as both a string (setting it to deleted) and an object (by setting two properties, deletedBy and deletedDate), which doesn't make sense.

您将状态属性视为一个字符串(设置为删除)和一个对象(通过设置两个属性,删除和deletedDate),这是没有意义的。

#1


3  

You're treating the status property as both a string (setting it to deleted) and an object (by setting two properties, deletedBy and deletedDate), which doesn't make sense.

您将状态属性视为一个字符串(设置为删除)和一个对象(通过设置两个属性,删除和deletedDate),这是没有意义的。