So i have this 2-layer query in node.js, each query could return multiple results. My code actually just ignores that for now. This is the best i can get, it seems working.
How to correct it please, i don't know how to callback for the 2nd one.
Also the db.close() is always called before the 2nd query finishes, even i have serialize().
var getInfo1Db = function(callback) { var db = new sqlite3.Database("DB.sqlite3");
var cnt = 0; var info1JsonObj = []; db.all("select * from Info1DB", function(err, rows) { db.serialize(function() { for(var ii=0, len=rows.length; ii<len; ii++) { var t2 = rows[ii].info1; var doorId = ... db.all("select * from DoorDB where ObjectID=" + doorId, function(err, row2) { if(err) { } else { var doorName = row2[0]... var info1JsonElem = { "DoorName" : doorName }; info1JsonObj.push(info1JsonElem); cnt++; if(cnt === rows.length) { callback(null, info1JsonObj); } } } ); // for the only door info based on door id } // for each row of info1 db.close(); // why this finishes before the 2nd db.all } ); // end of serialize });};