Sunday 30 April 2017

Wait till SQL query is completed NodeJS SQLite 3

I'm currently using SQLite 3 and I'm running a query that runs a callback function. This callback function stores all the results of the query in a function-level variable for access later on in the function. The callback along with the query seem to make the latter part of the function run thereby making the function-level variable empty. Is there a way to run the rest of the function only after the query completes?

var results = [];
var query = conn.query(q, function(error, result){

    for (var i = 0; i < result.rowCount; i++) {
            results.push(result.rows[i]);
        }
    console.log(results); // results is not empty here
} );


//  console.log(results); // results is empty here 



via learner561

No comments:

Post a Comment