在发送 POST 请求时收到 500 内部服务器错误。使用 Express 框架并发送 JSON 对象。
在TODO列表应用中按delete键删除任务时,没有选择任务会报错。如何处理这种情况。
app.post('/delete-todo', function(req, res) {
let ids = req.body.task;
// if single task is to be deleted
if (typeof(ids) == "string") {
Todo.findByIdAndDelete(ids, function(err) {
if (err) {
console.log("Error in deleting");
return;
}
});
} else { // if multiple task is to be deleted
for (let i = 0; i < ids.length; i++) {
Todo.findByIdAndDelete(ids[i], function (err) {
if (err) {
console.log("Error in deleting");
return;
}
});
}
}
return res.redirect('back');
});
<!-- Form to 'delete' task(s) from the database -->
<form id="delete1" action="/delete-todo" method="POST" >
<!-- Fetching data from the database -->
<% for(let i = todo_list.length - 1; i >= 0; i--) { %>
<div id="task">
<!-- CHECK BOX, DESCRIPTION AND CATEGORY INFORMATION OF THE TASK SHOW HERE -->
<input type="checkbox" id="check-box" name=task value="<%= todo_list[i]._id %>">
<!-- Each task of the todo list -->
<div class="task-container">
<span id="task-description">
<%= todo_list[i].description %>
</span>
<br>
<span id="task-date">
<i class="far fa-calendar-alt" aria-hidden="true"></i>
<%= todo_list[i].date %>
</span>
</div>
<div id="task-category">
<%= todo_list[i].category %>
</div>
</div>
<% } %>
</form>
</div>