I am trying to give myself a practice to just use pure node as my server and PG to connect to PostgreSQL. What I want to do here is to return the result after querying from database and know it is problem of async. So how could I return the value from the callback function inside client.query.
function showAllMovies(){
pg.connect(connectionString, function (err, client, done) {
if (err) {
console.log('Can not log into database');
} else {
console.log('Connect to database...');
client.query('SELECT * FROM movies', function (err, result) {
done(); // client idles for 30 seconds before closing
var result = JSON.stringify(result.rows[0].movie);
console.log(result);
return result;
});
}
});
pg.end();
}