我正在SQL数据库中更新两条以上的记录。实际上,我正在获得JSON格式的数据数组。
我在for循环中写了query,但是它发送了错误。下面是代码
app.post("/editDeviceLocation", (req, res) => {
var data_array = [];
try {
var device_data = req.body.nameValuePairs;
var total_device_data = device_data.length;
for (let i = 0; i < total_device_data; i++) {
db.select(
"mytag_device",
"device_mac_address",
`device_mac_address = '${device_data[i].device_mac_address}'`,
(data) => {
// console.log(data_array);
res.send(data_array);
}
);
}
} catch (error) {
res.send({ success: 0, data: error, message: "Error Occured" });
}
});
这是要发送的数据
{
"nameValuePairs":[
{
"device_mac_address":"FF:FF:97:03:45:51",
"user_id":"2",
"device_lat":"72.7602536",
"device_long":"21.1481166",
"time":"2021-03-01 16:29:20"
},
{
"device_mac_address":"FF:FF:97:03:45:511",
"user_id":"2",
"device_lat":"72.7602536",
"device_long":"21.1481166",
"time":"2021-03-01 16:29:20"
}
]
}
误差
D:MyTagMyTagnode_modulesmysqllibprotocolParser.js:437
throw err; // Rethrow non-MySQL errors
^
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
at ServerResponse.setHeader (_http_outgoing.js:558:11)
at ServerResponse.header (D:MyTagMyTagnode_modulesexpresslibresponse.js:771:10)
在数组中填满您想要发送的数据后,将响应发送到循环外。
app.post("/editDeviceLocation", (req, res) => {
**var data_array = [];**
try {
var device_data = req.body.nameValuePairs;
var total_device_data = device_data.length;
for (let i = 0; i < total_device_data; i++) {
db.select(
"mytag_device",
"device_mac_address",
`device_mac_address = '${device_data[i].device_mac_address}'`,
(data) => {
// console.log(data_array);
// res.send(data_array);
**data_array.push(data);**
}
);
}
} catch (error) {
res.send({ success: 0, data: error, message: "Error Occured" });
}
**res.send(data_array);**
});
如果您使用express。那么res.send()就不能在一个api上多次执行。填充一个数组并在循环外执行res.send,这样它就不会被多次调用。
当你有多个请求时,你应该这样做:
app.post("/editDeviceLocation", (req, res, next) => {
const allRequest = [];
const device_data = req.body.nameValuePairs;
const total_device_data = device_data.length;
for (let i = 0; i < total_device_data; i++) {
let req = db.select(
"mytag_device",
"device_mac_address",
`device_mac_address = '${device_data[i].device_mac_address}'`);
allRequest.push(req) // register all promises in an array
}
Promises.all(allRequest).then(data => {
// data will be an array and give you results in same manner
// data[0]; // for first one
// here you can send data to the user
res.send(data);
}).catch(e => console.log(e);
next(e);
)
});
我已经做了一些
app.post("/editDeviceLocation", (req, res) => {
var data_array = [];
try {
var device_data = req.body.nameValuePairs;
var total_device_data = device_data.length - 1;
for (let i = 0; i <= total_device_data; i++) {
db.select(
"mytag_device",
"device_mac_address",
`device_mac_address = '${device_data[i].device_mac_address}'`,
(data) => {
try {
if (data.status == 1 && data.data.length > 0) {
if (
data.data[0].device_mac_address ==
`${device_data[i].device_mac_address}`
) {
db.update(
"devicelocations",
`device_lat = '${device_data[i].device_lat}',device_long = '${device_data[i].device_long}',last_known_time = '${device_data[i].last_known_time}'`,
`device_mac_address = '${device_data[i].device_mac_address}' and user_id = '${device_data[i].user_id}'`,
(data) => {
data_array.push(data);
console.log(i + "/" + total_device_data);
if (i == total_device_data) {
res.send({
status: 1,
data: data_array,
message: "successfully Updated",
});
} else {
console.log("this is not last loop" + i);
}
}
);
} else {
res.send({ status: 0, data: [], message: "No Data Found" });
}
} else {
res.send({ status: 0, data: [], message: "No Data Found" });
}
} catch (error) {
res.send({ status: 0, data: [], message: "Erro Found" });
}
}
);
}
} catch (error) {
res.send({ success: 0, data: error, message: "Error Occured" });
}
});