The docs on https://node-postgres.com/guides/async-express give the example of:
const { Pool } = require('pg')
const pool = new Pool()
module.exports = {
query: (text, params) => pool.query(text, params),
}
My DB code is effectively the same, with some added logging:
const { Pool } = require('pg')
var config = {
user: process.env.DB_USER,
database: process.env.DB,
password: process.env.DB_PASSWORD,
host: process.env.DB_HOST,
port: process.env.DB_PORT,
max: 10,
idleTimeoutMillis: 30000,
};
const pool = new Pool(config);
module.exports = {
query: (text, params) => {
console.log(`sql: ${text}, params: ${params}`);
return pool.query(text, params);
}
}
This works fine when called with:
const result = await db.query('SELECT * FROM users WHERE email = $1 AND password = $2;',
[req.body.email, req.body.password]);
When I make a mistake in my SQL, I get the warning:
(node:6345) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:6345) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
POST /verify - - ms - -
post /verify
(node:6345) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'id' of undefined
at router.post (/home/fadedbee/myapp/routes/index.js:93:58)
...
at router (/home/fadedbee/myapp/node_modules/express/lib/router/index.js:47:12)
(node:6345) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code. is what worries me.
How should I change my DB code fix this?