Tuesday 6 June 2017

Why my function returns an empty array which contains data inside the callback function? [duplicate]

This question already has an answer here:

I am working on a nodejs script, I want that the "gettables()" function returns an array which contains the tables name from mysql database. But the problem is that the function return an empty array.

Here is my code :

function  mysql_getTables(){
     var tables =[];
    //connecting to mysql database
    connectToSql('localhost','root','','sbs_managementapp');
    mysql_connection.query("SELECT table_name FROM information_schema.tables where table_schema='sbs_managementapp';",function (err, rows, fields){
        if (!err){
            for(var i =0; i<rows.length;i++){
                tables[i] = rows[i]['table_name'];
            } 
        }
        else
        console.log('Error while performing Query.');
});
    mysql_connection.end();
    return tables;
}

Here is the returned array : https://i.stack.imgur.com/SN0xN.png



via elhamzaoui

No comments:

Post a Comment