首页 > 解决方案 > 我无法更改数据库中的值 - 数组

问题描述

我需要更新评论状态,即“批准”字段的名称。为此,我创建了 AJAX 脚本,后端似乎也是正确的,因为它在单击按钮后会收到一条消息。我不知道为什么我不能将“批准”值保存到数据库中。请帮忙。我使用 NodeJS、MongoDB、Express 和 EJS 作为前端。我的数据库: 在此处输入图像描述

我的前端:

 <% post.comments.forEach(function (comment) { %>

    <tr>
        <td> <%= comment._id %> </td>
        <td> <%= comment.username %> </td>
        <td> <%= comment.comment %> </td>

        <form method="post" onsubmit="return  doApprove(this);">
            <input type="hidden" name="post_id" value="<%= post._id %>" />
            <input type="hidden" id="comment_id" name="comment_id">
                 <td> <input class="approve-comment" type="checkbox" name="approve" value="true">
                <button class="btn btn-info" value="Approve"/>
                 </td>
        </form>
        <% }) %>
    </tr>

</table>

</div>
<script>
function doApprove(form) {
    var formData= {approve:form.approve.value};
    $.ajax({
        url: "/do-edit-comment",
        method: "POST",
        data: formData,
        success: function (response) {
            formData._id =response._id;
            alert(response.text);
        }
    });
    return false;
}
</script>

我的后端:

app.post("/do-edit-comment", function (req,res) {
    blog.collection("posts").updateOne({
        "_id": ObjectId(req.body.id),
        "comments._id": ObjectId(req.body.id)
    },{
        $set: {
            "comments": {approve: req.body.approve}

        }
    }, function (error,document) {
        res.send({

            text: "comment approved"

        });
    });
});

标签: node.jsmongodbexpress

解决方案


要更新数组中的单个元素,您需要的是$[<identifer>]数组更新运算符。对于您问题中描述的场景,服务器中的更新查询应该是这样的:

blog.collection("posts").updateOne(
    { "_id": ObjectId(req.body.post_id), },
    { $set: { "comments.$[comment].approve": req.body.approve } }, 
    { arrayFilters: [{ "comment._id": ObjectId(req.body.commentId) }] },
    function (error, document) {
        res.send({
            text: "comment approved"
        });
    }
)

编辑(前端问题/修复)
所以我认为前端代码也存在一些问题。此编辑尝试解释/修复这些问题。

问题:
1. 后端期望commentId请求对象(req.body.commentId)中的 a 能够识别要更新的评论,但您没有从前端发送该评论。
2.后端需要帖子的id来唯一标识要更新的帖子,但您不是从前端发送的。
3.approve前端发送的表单数据中的值是一个字符串,它总是"true"。这不是您想要的,您想要发送一个布尔值(truefalse),具体取决于复选框是否被选中。

修复:
将表单模板更新为:

<form method="post" onsubmit="return doApprove(event);">
    <input type="hidden" name="post_id" value="<%= post._id %>" />
    <input type="hidden" id="<%= comment._id %>" name="comment_id" />
    <td> <input class="approve-comment" type="checkbox" name="approve" value="true">
        <button class="btn btn-info" value="Approve"/>
    </td>
</form>

更改:
-doApprove现在调用附加到表单提交事件的处理程序,event而不是this. - 我将元素
的 id 属性的值更新为实际值。input#name="comment_id"comment._id

在脚本标签中,将doApprove函数更新为:

function doApprove(event) {
  event.preventDefault();
  const form = event.target;
  var formData = {
    approve: form.approve.checked, // form.approve.checked would be true if the checkbox is checked and false if it isn't
    post_id: form.post_id.value, // The value of the input#name=post_id element
    commentId: form.comment_id.id, // The id attribute of the input#name=comment_id element
  };
  $.ajax({
      url: "/do-edit-comment",
      method: "POST",
      data: formData,
      success: function (response) {
          formData._id =response._id;
          alert(response.text);
      }
  });
  return false;
}

我希望这会有所帮助。


推荐阅读