Wednesday, 24 May 2017

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

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

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 ant the http requests are working, my terminal is just getting flooded with these warnings.



via Luke Schlangen

No comments:

Post a Comment