node.js - 我无法更改数据库中的值 - 数组
问题描述
我需要更新评论状态,即“批准”字段的名称。为此,我创建了 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"
});
});
});
解决方案
要更新数组中的单个元素,您需要的是$[<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"
。这不是您想要的,您想要发送一个布尔值(true
或false
),具体取决于复选框是否被选中。
修复:
将表单模板更新为:
<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;
}
我希望这会有所帮助。
推荐阅读
- ruby-on-rails - “密钥必须是 16 字节”AES-128 CBC,带有 MD5 密钥+IV
- sql - [Microsoft][ODBC SQL Server Driver][SQL Server]关键字“AS”附近的语法不正确。QODBC3:无法执行语句”
- java - 如何从现有的动态代理中获取“代理”对象
- typescript - 从返回值推断类型
- ios - 如何使用 `var shouldAutorotate` 旋转屏幕返回 false
- java - 使用改造 2 将图像从画廊/相机上传到服务器(okhttp 问题)
- php - 为 Sonata UserBundle 添加字段
- python - 如何将两个非字符串加在一起......?
- bash - 并行处理文件组,然后使用 slurm 串行计算
- java - 在类路径资源中定义名称为“handlerExceptionResolver”的 bean 创建错误