3

This error seems to be coming up on every http request I make. I'm not totally sure where it's coming from?

(node:39390) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 22): ReferenceError: client is not defined

There is no line number in the error, but here is a sample of code that seems to be causing it:

try {
    var client = await pool.connect();
    await client.query(queryStatement, queryArgumentsArray);
    res.sendStatus(200);
} catch (e) {
    console.log('Error adding updating subvendor availability data, UPDATE SQL query task', err);
    res.sendStatus(500);
} finally {
    client && client.release && client.release();
}

It first I thought it must be coming from my finally block (maybe client was out of scope), but I added and if statement to explicitly prevent attempting to call client.release if it doesn't exist:

if (client) { client && client.release && client.release() };

I am still getting this error, so I feel like it must be coming from these lines.

var client = await pool.connect();
await client.query(queryStatement, queryArgumentsArray);
res.sendStatus(200);

Am I misunderstanding how to use async? To be clear, the code is functioning well and the http requests are working (responding correctly to the requests), my terminal is just getting flooded with these warnings.

Here is a simplified version of the full route:

var express = require('express');
var router = express.Router();
var pool = require('../modules/pg-pool'); // brings in pg-pool created in another module

// This route updates the availability for a user
router.put('/updateAvailability', async (req, res) => {
    var userId = req.decodedToken.userSQLId;
    var subvendorId = req.headers.subvendor_id;
    var availability = req.body;

    var queryStatement = 'UPDATE status SET status=$3 WHERE userId=$2';
    var queryArgumentsArray = [availability.status, userId ];

    try {
        var client = await pool.connect();
        await client.query(queryStatement, queryArgumentsArray);
        res.sendStatus(200);
    } catch (e) {
        console.log('Error updating subvendor availability data, UPDATE SQL query task', err);
        res.sendStatus(500);
    } finally {
        client && client.release && client.release();
    }
});

module.exports = router;
Luke Schlangen
  • 3,722
  • 4
  • 34
  • 69
  • Is there no line number in the exception? – Bergi May 25 '17 at 02:01
  • That code looks fine. Is this the exact, whole code? Are you using the term `client` anywhere else (grep for it)? – Bergi May 25 '17 at 02:03
  • @Bergi Correct, there is no line number. I updated with the full route (I simplified the query a little because it's not the focus). Any clues from here? – Luke Schlangen May 25 '17 at 14:22
  • @LukeSchlangen try [pg-promise](https://github.com/vitaly-t/pg-promise) instead. If you no longer see the issue, then the problem is inside the `node-postgres` driver. – vitaly-t May 25 '17 at 14:40
  • This is really weird. How are you using ES8 `async`/`await`, with a transpiler or the native one? If the former, maybe we should have a look at the transpilation output. – Bergi May 25 '17 at 17:03
  • Native node.js, so I don't have transpilation output. Specifically node v7.9.0 – Luke Schlangen May 25 '17 at 19:59
  • Hm, especially then I'd expect a better stack trace. Do you have a `process.onunhandledrejection` handler? Try inspecting the error more closely. Alternatively, file a bug with node (or two bugs: one about the unhelpful error message, another about the non-working script). – Bergi May 26 '17 at 00:26

1 Answers1

2

All credit here goes to brianc the creator of node-postgres who answered my question here after I received the suggestion that it might be a problem with that library (doesn't seem like it was). I simply needed create the client outside of the try-catch

var client = await pool.connect()
try {
  await client.query(...)
  res.sendStatus(200)
} catch { ...} 
finally {

}

His full answer can be found here: https://github.com/brianc/node-postgres/issues/1301

Luke Schlangen
  • 3,722
  • 4
  • 34
  • 69