https://node-postgres.com/guides/async-express上的文档给出了以下示例:
const { Pool } = require('pg')
const pool = new Pool()
module.exports = {
query: (text, params) => pool.query(text, params),
}
我的数据库代码实际上是相同的,但添加了一些日志记录:
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);
}
}
当使用以下方法调用时,这可以正常工作:
const result = await db.query('SELECT * FROM users WHERE email = $1 AND password = $2;',
[req.body.email, req.body.password]);
当我在 SQL 中出错时,我会收到警告:
(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.
是我担心的。
我应该如何更改我的数据库代码来解决这个问题?