dimanche 3 avril 2016

How can an iteration wait for a sqlite3 query in node.js?

I have something like this:

async.forEach(JSONfile,function(item,next){                      
  insertData(item.guid,item.size,item.price,item.latitude,item.longitude,item.property_type,function(){                    
    next();
   });
//code after the insert into db

});

And the function :

function insertData(guid,price,size,longitude,latitude,property_type,callback){
 db.serialize(function() {
  var stmt = db.prepare("INSERT INTO casas VALUES (?,?,?,?,?,?)");
  stmt.run(guid,price,size,longitude,latitude,property_type);
 },function(){
    callback();
 });
}

The issue is that async.forEach always iterates over the JSON and each iteration doesn't wait for the database to finish that query. The point is to get one insert by iteration, and what I'm getting is a lot of queries stacked at the end of the execution. Any help will be appreciated. Thank you

Aucun commentaire:

Enregistrer un commentaire