[英]NodeJS response has been sent before MySQL query execution
Here is my NodeJS code, I want to return csvFile array after the query executed, but I first return an empty array and then execute the query. 这是我的NodeJS代码,我想在执行查询后返回csvFile数组,但是我首先返回一个空数组,然后执行查询。 I call getDetection function in getSummary function.
我在getSummary函数中调用getDetection函数。 Both functions are executing a query.
这两个函数都在执行查询。 Can anybody please help me?
有人可以帮我吗?
function getSummary(req, res) {
var result = [];
var csvFile = new Array();
//console.log(csvFile);
pool.getConnection(function(err, connection){
if (err) {
res.json({"code" : 100, "status" : "Error in connection database"});
return;
}
//var result = [];
var option = req.body;
//console.log(option.type);
connection.query("SELECT DISTINCT name FROM dgh WHERE name LIKE ? ", ['%' + option.type.toUpperCase() + '%'],
function(err, rows, fields) {
connection.release();
if (err) {
throw err;
} else {
for (var i = 0; i < rows.length; i++) {
var row = rows[i].name;
result.push(row);
getDetection(result[i], function(result) {
if (err) {
throw err;
} else {
var count = result.count;
var maxD = result.maxD;
var array = new Array(count, maxD);
csvFile.push(array);
//console.log(csvFile);
}
});
}
}
});
})
console.log(csvFile);
res.csv(csvFile);
}
getDetection function: getDetection函数:
function getDetection(filename, callback) {
pool.getConnection(function(err, connection){
if (err) {
res.json({"code" : 100, "status" : "Error in connection database"});
return;
}
connection.query("SELECT hour, minute, second FROM dgh WHERE name = ?", [filename],
function(err, rows, fields){
connection.release();
if (err) {
throw err;
} else {
var count = 0;
var maxD = 0;
for (var i = 1; i < rows.length; i++) {
var time1 = rows[i].hour * 3600 + rows[i].minute * 60 + rows[i].second;
var time2 = rows[i - 1].hour * 3600 + rows[i - 1].minute * 60 + rows[i].second;
if (time2 - time1 > 60) {
count = count + 1;
maxD = Math.max(maxD, time2 - time1);
}
}
callback({count:count, maxD:maxD});
}
})
})
}
Node.js is asynchronous. Node.js是异步的。 If getDetection is an asychronous function for loop will not execute as you expect.
如果getDetection是一个异步函数,则for循环将无法按预期执行。
for (var i = 0; i < rows.length; i++) {
var row = rows[i].name;
result.push(row);
getDetection(result[i], function(result) {
if (err) {
throw err;
} else {
var count = result.count;
var maxD = result.maxD;
var array = new Array(count, maxD);
csvFile.push(array);
//console.log(csvFile);
}
})
}
}
As of right now, for loop will just pass through getDetection function and finish iteration. 截至目前,for循环将仅通过getDetection函数并完成迭代。 This causes the variable csvFile to be empty.
这将导致变量csvFile为空。
I suggest you install the async package using npm . 我建议您使用npm安装异步软件包。
npm install --save async
After installing 安装后
async.each(rows, function(name, callback) {
result.push(name)
getDetection(name, function(item){
if (err) throw err
var count = item.count
var maxD = item.maxD
var array = new Array(count, maxD)
csvFile.push(array)
callback()
})
}, function(err){
if (err) {
//Handle error here that occurred during iteration
} else { //finished all iteration
console.log(csvFile)
res.csv(csvFile)
return
}
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.