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. I call getDetection function in getSummary function. 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:
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. If getDetection is an asychronous function for loop will not execute as you expect.
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. This causes the variable csvFile to be empty.
I suggest you install the async package using 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
}
})
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.