Why is resultofgetCauses undefined? I'm not sure if the function is not returning currentCauses or if it not being assigned to resultofgetCauses . . . or if this has something to do with asynchronicity.
function getCauses(){
var currentCauses;
client = pg.connect(connectionString, function(err, client, done){
if(err) console.log(err);
client.query('SELECT * FROM causes', function(err, result){
//console.log(result.rows);
console.log('poo');
currentCauses=result.rows;
//console.log(currentCauses);
});
});
return currentCauses;
};
var resultofgetCauses = getCauses();
currentCausesisundefinedasresultofgetCausesgets assigned beforepg.connecthas time to finish. That question has an in-depth solution to your problem.pg.connectandclient.query