首页 > 解决方案 > 错误:在 BoundPool.end 上多次调用池结束

问题描述

当我尝试使用 pg-promise 从 PostgreSQL 获取数据时,它会抛出一个错误

错误:在 Database.pool.end.cb (/workspace/node_modules/pg-promise/lib) 的 BoundPool.end (/workspace/node_modules/pg-pool/index.js:381:19) 上多次调用池结束/database.js:133:31)

app.get("/route/:id", async (req: any, res: any) => {
    const key = req.params.id;
    let status: number;
    let response: any = {};
    console.log(key);
    await db.any('SELECT * FROM "schema"."my_table" WHERE key = $1', [key])
        .then(data => {
            status = 200;
            response = {
                'status': 'success',
                'data': JSON.stringify(data)
            }
        })
        .catch(error => {
            status = 406;
            response = {
                'status': 'failed',
                'error': error.message,
                'errorCode': error.code
            }
        })
        .finally(db.$pool.end);
    res.status(status).send(response);
});

我应该什么时候结束连接池?响应成功时会自动结束吗?

标签: node.jspg-promisenode-postgres

解决方案


推荐阅读