Reputation: 67
I run select query using pg ,node.js and hapi, and its work.
but how can i return the rows?
The selected:
var client = new pg.Client(conString);
client.connect(function(err,done) {
if (err) {
return console.error('could not connect to postgres', err);
}
})
function fun(query){
client.query(query, function (err, result,done) {
if (err) {
console.error('error running query', err);
return;
}
else{
result.rowAsArray=true;
console.log(result.rows[0]);
data=result.rows;
}
});
return data
}
}
but its return a object with nothing,
What is the right way?
Thank you.
Upvotes: 2
Views: 1163
Reputation: 8105
You need to loop through the result set.
function fun(query) {
var data = [];
var sql = client.query(query, function(err, result) {
if (err) {
console.error('error running query', err);
return;
}
});
sql.on('row', function(row) {
console.log(row);
data.push(row);
}
return data;
}
Upvotes: 1