I am creating a function that will return me the result of a DB query. The problem is when I am calling the main function, the query function within the main function(getSessionCode) is not getting executed first. I tried the Promise manager approach as well as the callbacks but none of those seems to be working.
Code:
dbConnectionObject = require('./DatabaseHandler.js');
dbConnect = dbConnectionObject.getdatabaseConnection();
var sessioncode;
function getSessionCode(emailaddress){
var queryBuild = "select <column name> from <table_name> where email="+ "\'"+ emailaddress +"\'";
var responseArray;
dbConnect.query(queryBuild,(err, res) =>{
if(err)
{
console.log(err);
}
else if(res){
responseArray=res.rows;`enter code here`
dbConnect.end();
console.log(responseArray);
return responseArray;
}
});
}
sessioncode = getSessionCode('gautam.pruthi@abc.com');
console.log(sessioncode);