0

i am developing a node.js applicaton where i am using the node-mysql connector. This is how i am retrieving the data:

var queryString2 = "SELECT * FROM table WHERE domain = 'xyz';"
conn.query(queryString2, function (error,results2)
    {
        if(error)
            {
                throw error;
            }
            else
        {
            abc = results2;
            console.log(abc);  //this works
        }
    }
)

console.log(abc); //this does not work

My question is how do i access the value of abc outside of the conn.query function?

Vasa
  • 45
  • 1
  • 8
  • 1
    By the time `console.log()` is invoked, success callback is not executed hence `abc` is undefined.. – Rayon Feb 15 '16 at 04:28

1 Answers1

0
// Import events module
var events = require('events');
// Create an eventEmitter object
var eventEmitter = new events.EventEmitter();

Emit the event in your else

eventEmitter.emit('got-result',results2);

Handle or listen event where you want to access result2 value

eventEmitter.on('got-result', function(data){
console.log(data);
});
Hashir Hussain
  • 614
  • 4
  • 8
  • 27