如何解决尝试使用 node-postgres 事务时出现问题 - 使用 async/await 的池化客户端
我正在尝试使用 node-postgres 事务 - 请参阅 here
我特意尝试使用带 async/await 的池客户端,但在这样做时收到错误。
我在下面使用的 db.js
文件具有以下内容:
const Pool = require("pg").Pool;
const pool = new Pool({
user: "user",password: "password",host: "localhost",port: 5432,database: "mydb"
});
module.exports = pool;
这是我的代码片段:
const express = require("express");
const app = express();
const cors = require("cors");
const pool = require("./db");
app.post("/process-job",async (req,res) => {
const client = await pool.connect()
将 const client = await pool.connect()
放入我的 app.post("/process-job"
后,出现以下错误:
debug('dispatching %s %s',req.method,req.url);
^
TypeError: Cannot read property 'method' of undefined
如果我删除 (req,res)
,它就可以正常工作,即没有错误。
任何人都可以协助解决问题吗?
解决方法
发现实际问题是我还需要将 pg.Client
作为我上面的 db.js
文件的一部分引入,现在看起来像这样:
const { Pool,Client } = require("pg");
const pool = new Pool({
user: "user",password: "password",host: "localhost",port: 5432,database: "mydb"
});
module.exports = pool;
然后我可以使用:const client = await pool.connect()
没有错误。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。