【发布时间】:2020-09-16 22:26:34
【问题描述】:
我正在发送一个包含数据作为对象数组的请求:
[
{id: "1"},
{id: "2"},
{id: "3"}
]
我使用JSON.stringify(),我的 req.body 看起来像这样:
{ '{"id":"1"},{"id":"2"},{"id":"3"}': '' }
现在我想遍历 req.body 并获取所有 ID,以便我可以从 SQL DB 中删除它们。 我正在使用续集。 后端:
exports.deleteIds = (req, res, next) => {
console.log(req.body)
//here should be loop so i can delete all the ids one by one.
Model.destroy({
where: {
id:
}
})
}
发布请求(客户端):
let ids = []
//maybe here is the problem?
for (var i = 0; i < selectedRow.length; i++) {
ids.push({id:selectedData[i].id})
}
let Url = "/admin/deleteIds"
let data = JSON.stringify(ids)
event.preventDefault();
$.post(Url, data, function (data, status) {
}).done(function (res) {
if (res.ids.length == 0) {
$('#mainContent').html('<h1>0 users found</h1>')
}
})
.fail(function (err) {
console.log(err.responseJSON.message)
})
【问题讨论】:
-
您使用的是哪个数据库库?,它可能是一个内置选项
-
@Itamar 我正在使用 Sequelize
-
在下面查看我的答案
标签: javascript node.js loops express