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

Viewed 7580

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;
1 Answers
Related