Q:

Nodejs change global variable inside mysql query

i'm trying to create a nodejs server but the following code is not working any advice?
var app = require('http').createServer();
var io = require('socket.io')(app);
var mysql = require('mysql');

var dblists = new Array();
pool=mysql.createPool({host : host,user : user,password: pass });   
pool.getConnection(function(err, connection){
    connection.query("use nodejs");
    connection.query('SELECT db_name,domain_name FROM tbl_dblist;',
    function (err, rows,f) {
        rows.forEach(function (entry) {
            dblists[entry["domain_name"]] = entry["db_name"];
        });
    });
});

console.log(dblists);   

Thanks in advance]
The whole code: 
var dblists = new Array();

app.listen(3000);
var xsocket = io.listen(app);
//console.log('Listen On Port 3000');
var pool=mysql.createPool({host : host,user : user,password: pass });   

pool.getConnection(function(err, connection){
    connection.query("use nodejs");
    connection.query('SELECT db_name,domain_name FROM tbl_dblist;',
    function (err, rows,f) {
        rows.forEach(function (entry) {
            dblists[entry["domain_name"]] = entry["db_name"];
        });
    });
});

xsocket.set('authorization', function (handshakeData, callback) {
    callback(null, true);
});

xsocket.on('connection', function (conn) {
    xsocket.emit('list',dblists);
});

A:

getConnection is asynchronous, so your console.log fires before you get any values in your array. 
If you put the console.log inside the method, it will work as epxected:
xsocket.on('connection', function (conn) {    
    pool.getConnection(function(err, connection){
        connection.query("use nodejs");
        connection.query('SELECT db_name,domain_name FROM tbl_dblist;',
        function (err, rows,f) {
            rows.forEach(function (entry) {
                dblists[entry["domain_name"]] = entry["db_name"];
            });
            xsocket.emit('list', dblists);
        });
    });        
});

