I'm seeking assistance with the code snippet below. I am encountering difficulties in accessing the "result" variable outside of the selectCb function scope. While "result" is properly assigned and functional within the selectCb scope, it becomes inaccessible beyond that boundary.
function queryDB(client, queryString) {
result = ''; //initialize global variable
client.query(queryString, function selectCb(error, results, fields) {
if (results.length > 0) result = results[0];
console.log(result['id']); //SUCCESSFUL OUTPUT HERE
});
client.end();
console.log(result['id']); //UNABLE TO FETCH - RETURNS UNDEFINED
return result; //deliver complete result array
};
var data = queryDB(client,"select id from table");
console.log(data['id']) //INACCESSIBLE RESULT - RETURNS UNDEFINED;