i'm trying to use sqlite3 in node.js script, i'm using this library:
https://github.com/mapbox/node-sqlite3
but i want that the call at the database is synchronous and not asynchronous, this is an example of the code i use:
var tests = [];
db.serialize(function() {
db.each("SELECT lot_id, status FROM TestTable ORDER BY lot_id ASC", function(err, row) {
tests.push(row.status);
console.log("Read "+row.lot_id+" state: "+row.status);
});
});
db.close();
for (i = 0; i < tests.length; i++) {
//Do something
}
when the execution arrives at the for loop, go over because the tests array is still empty, how i can perform this instruction sequentially?
thanks
Execute the for-loop in the complete callback
var tests = [];
db.serialize(function() {
db.each("SELECT lot_id, status FROM TestTable ORDER BY lot_id ASC", function(err, row) {
tests.push(row.status);
console.log("Read "+row.lot_id+" state: "+row.status);
}, function() { // this callback is executed when the query completed
for (i = 0; i < tests.length; i++) {
// Do something
}
});
});
db.close();