UnhandledPromiseRejectionWarning: Unhandled Promise Rejection (Rejection ID: 22): ReferenceError: Client Undefined
This error seems to work for every request http
I make. I'm not really sure where he comes 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 some sample code that appears to be calling 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();
}
At first I thought it should come from my finally block (maybe client
out of scope), but I added and if the statement explicitly forbids trying 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 should come from these lines.
var client = await pool.connect();
await client.query(queryStatement, queryArgumentsArray);
res.sendStatus(200);
Don't understand how to use async? To be clear, the code is working well and the requests http
are working (reacting to requests correctly), my terminal is just flooding with these warnings.
Here's 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;
source to share
All information here is from the brianc
creator node-postgres
who answered my question here after I received a suggestion that it might be a problem with this library (it doesn't seem to be the case). I just needed to create a client outsidetry-catch
var client = await pool.connect()
try {
await client.query(...)
res.sendStatus(200)
} catch { ...}
finally {
}
His complete answer can be found here: https://github.com/brianc/node-postgres/issues/1301
source to share