0

I am pooling connections to my Postgres DB with pg-pool. I am having trouble, however, sending the results of my queries to the frontend. I assumed that I could use res.send as usual but it is not recognized (I also tried return). Here is my function:

exports.featList = function (req, res) {
 pool.connect(function(err, client, done) {
  if (err) {
   return console.error('error fetching client from pool', err);
  }
  client.query('SELECT * FROM book WHERE featured=true', function (err,res) {
   var json = JSON.stringify(res.rows);
   return json;
   client.release();
 });
});
eabates
  • 878
  • 1
  • 8
  • 22

1 Answers1

0

It seems that is not returning the results because in the callback of the client.query you are redeclaring the res variable so you can't access the response object. If you rename this variable from res to results it will be able to return the results to the client:

exports.featList = function (req, res) {
  pool.connect(function (err, client, done) {
    if (err) {
      return console.error('error fetching client from pool', err);
    }
    client.query('SELECT * FROM book WHERE featured=true', function (err, results) {
      var json = JSON.stringify(res.rows);
      res.json(json);
      client.release();
    });
  });
}
Stavros Zavrakas
  • 3,045
  • 1
  • 17
  • 30
  • Oops, thanks, I actually just mistyped that (had it right in the actual code). I am starting to think that my issue is including the sending code within `client.query`. It needs to go outside I believe, just working on where. – eabates Oct 26 '16 at 13:57
  • @eabates the approach feel wrong because you should not create a connection on every request and it should not live in a middleware. You have to move the `pool.connect` into another file/lib that will be the database connector and when you boot the application to connect to the db. This file/lib should have a function that will return the client when needed (in the middleware in your case) – Stavros Zavrakas Oct 26 '16 at 14:09
  • Actually I had to do `client-release` before `res.send`. – eabates Oct 26 '16 at 14:09
  • Hm, I do connect to the database in a separate module but as far as I understand `pool.connect` is the way to put a single client into the pool (and then remove it if it is "idle"). I have been monitoring my connections with `pg_stat_activity' and they do ebb and flow properly... – eabates Oct 26 '16 at 14:25
  • I wasn't aware of the node-pg-pool. What is the difference with the node-postgres pool? I thought that this is what you were using. Did the answer above resolve your issue? – Stavros Zavrakas Oct 26 '16 at 14:32
  • Hm, I found the developer's description on NPM: "node-postgres uses pg-pool to manage pooling. It bundles it and exports it for convenience. If you want, you can require('pg-pool') and use it directly - it's the same as the constructor exported at pg.Pool." Well, I appreciate your finding the inconsistency with the typo but it was really the (somewhat counterintuitive) order of `client-release` before `res.send` that fixed it! – eabates Oct 26 '16 at 14:52
  • As soon as you don't do `return res.json(json)` the above version should work as well. If you do `return res.json(json);` and then `client.release();` the results will be returned to the client, the execution will stop and the connection will never be released. – Stavros Zavrakas Oct 26 '16 at 15:00