node-postgres icon indicating copy to clipboard operation
node-postgres copied to clipboard

Getting a full result set in one go

Open grantpalin opened this issue 10 years ago • 4 comments

I've been using this library for an API layer for querying and returning data. However I'm at a point where I want to retrieve data (from a SELECT query) and immediately process the results, in combination with the results of two other similar queries.

It seems to me this would be easiest if I can get a full copy of the result set - I tried the technique shown on Query.row, but while looping within the query body and console.logging the individual rows did work, accumulating them in an array defined externally apparently does not.

Is there a straightforward way to suck the full row set into an array? It's not a large resultset in terms of rows or columns so that's not an issue, I just need to be able to get the whole thing at once.

var result = [];
var query = client.query('SELECT name, age as user_age FROM users');
query.on('row', function(row, result) {
    console.log(row); // <-- works
    result.addRow(row);
});
console.log(result); // [], expecting array of 5 objects

grantpalin avatar Sep 12 '16 20:09 grantpalin

yah - the whole query.on('row', ... thing is something I never use myself to be honest, I use the callback/promise style:

client.query('SELECT name, age as user_age FROM users', function(err, res) {
  console.log(res.rows)  // <--- all your rows are there in the array
})

There is extensive documentation covering this I believe - check it out. here, here, and here

brianc avatar Sep 13 '16 15:09 brianc

Thanks for your response. I did change to using the callback approach, e.g.

var doc = new PDFDocument();

client.query('query 1', function(err1, result1) {
    client.query('query 2', function(err2, result2) {
        client.query('query 3', function(err3, result3) {
            // processing happens here
            // writing to PDF also
        });
    });
});

doc.end();

I have to do this because I'm working with three sets of related data, processing the results and generating a PDF (using pdfkit). It seems to me so far that the most reliable way to have the full data available is within the nested callbacks.

The above works to a point - I can do all the processing I need to do, but due to the async nature of the data retrieval, the PDF generation is being completed before the data has finished processing and writing, resulting in an error and no PDF. Any thoughts?

grantpalin avatar Sep 13 '16 15:09 grantpalin

Try putting doc.end in the final callback. You might want to use an asynchronous library like async to make doing multiple async things easier

On Tuesday, September 13, 2016, Grant Palin [email protected] wrote:

Thanks for your response. I did change to using the callback approach, e.g.

var doc = new PDFDocument();

client.query('query 1', function(err1, result1) { client.query('query 2', function(err2, result2) { client.query('query 3', function(err3, result3) { // processing happens here // writing to PDF also }); }); });

doc.end();

I have to do this because I'm working with three sets of related data, processing the results and generating a PDF (using pdfkit). It seems to me so far that the most reliable way to have the full data available is within the nested callbacks.

The above works to a point - I can do all the processing I need to do, but due to the async nature of the data retrieval, the PDF generation is being completed before the data has finished processing and writing, resulting in an error and no PDF. Any thoughts?

— You are receiving this because you modified the open/close state. Reply to this email directly, view it on GitHub https://github.com/brianc/node-postgres/issues/1128#issuecomment-246730142, or mute the thread https://github.com/notifications/unsubscribe-auth/AADDoYddABwS_GHILc-zsVhYs04Vq7kxks5qpseCgaJpZM4J6-ww .

brianc avatar Sep 13 '16 17:09 brianc

I ultimately worked around this issue by using parameters to the PDF generating function to hold the data, rather than retrieving data and then consuming it within the same function. Possibly better design, but it works anyway.

grantpalin avatar Sep 16 '16 21:09 grantpalin