Saturday, 6 February 2016

how to detect 0 records returned with postgres?

I'm trying to see if a postgres query has zero results using node-pg.Here is the code:app.get('/user/:id', function(req, res) { //Get a Postgres client from the connection pool var results = []; pg.connect(connectionString, function(err, client, done) { // Handle connection errors if(err) { done(); res.render('error fetching client from pool', err); } // SQL Query > Select Data var query = client.query("SELECT * FROM public.users WHERE uid = ($1);", [req.params.id]); console.log(client.query[0]); //handle query error query.on('error', function(error) { done(); console.log('error:'); res.send(error); }); **//handle empty return** // Stream results back one row at a time query.on('row', function(row, result) { results.push(row); }); // After all data is returned, close connection and return results query.on('end', function(result) { done(); return res.json(results); }); }); }); Where it says "handle empty return," above, I would just LOVE to find a way to detect if there is zero return, and the documentation for this is just sanity-blastingly inadequate.Can anyone help out?

Submitted February 06, 2016 at 09:08PM by lawstudent2

No comments:

Post a Comment