Please help with the below code. I am having trouble returning the "result" variable within the selectCb function. "result" is assigned and works fine within the selectCb scope, however outside the scope I cannot access it.
function queryDB(client,queryString) {
result = ''; //declare global variable
client.query(queryString, function selectCb(error, results, fields) {
if (results.length > 0) result = results[0];
console.log(result['id']); //WORKS HERE
});
client.end();
console.log(result['id']); //DOES NOT WORK - UNDEFINED
return result; //return result array
};
var data = queryDB(client,"select id from table");
console.log(data['id']) //DOES NOT WORK - UNDEFINED;