gpt4 book ai didi

javascript - 错误 [ERR_HTTP_HEADERS_SENT] : Cannot set headers after they are sent to the client (multiple data entries)

转载 作者:行者123 更新时间:2023-12-03 01:14:41 24 4
gpt4 key购买 nike

因此,我尝试插入然后更新数据,数据库已根据预期查询成功更新,并且能够接收对客户端的响应,但在服务器上收到上述错误,不知道如何调试它,我'我尝试查看之前的相关问题,但没有成功。

var urlencodedParser = bodyParser.urlencoded({ extended: false });
router.post('/createprovision', urlencodedParser,(req, res) => {
let serials = [];
for (let i = 0; i < req.body.length; i++){
serials.push(req.body[i][1]);
}

let sql = 'INSERT INTO provision (employee_id, serial_no, provision_date) VALUES ?';
connection.query(sql, [req.body],(err, results) => {
for(let i = 0; i < serials.length; i++){
let sql2 = `UPDATE inventory SET ? WHERE serial_no = '${serials[i]}'`;
connection.query(sql2, [{status: "assigned"}],(err, results) => {
if (err) throw err;
return res.send("provision success!");
});
}
});
});

ERR_MSG:

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client (multiple data entries)

最佳答案

您已将 return res.send("provision success!"); 放入 for 循环内,这意味着您在第一次迭代中向客户端发送响应,然后您尝试再次发送响应,但 header 已发送。将其放在函数末尾,仅在处理所有serials后发送响应。

关于javascript - 错误 [ERR_HTTP_HEADERS_SENT] : Cannot set headers after they are sent to the client (multiple data entries),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52071905/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com