我将Typeorm与Postgresql数据库一起使用。我正在测试一个运行findOne查询的函数,它将引发以下错误:
{ QueryFailedError: Connection terminated
at new QueryFailedError (/Users/juanjosegutierrez/projects/banking-server/node_modules/typeorm/error/QueryFailedError.js:27:28)
at Query.callback (/Users/juanjosegutierrez/projects/banking-server/node_modules/typeorm/driver/postgres/PostgresQueryRunner.js:212:38)
at Query.Object.<anonymous>.Query.handleError (/Users/juanjosegutierrez/projects/banking-server/node_modules/pg/lib/query.js:142:17)
at process.nextTick (/Users/juanjosegutierrez/projects/banking-server/node_modules/pg/lib/client.js:59:13)
at process._tickCallback (internal/process/next_tick.js:61:11)
message: 'Connection terminated',
name: 'QueryFailedError',
query:
'SELECT "Contact"."id" AS "Contact_id", "Contact"."type" AS "Contact_type" FROM "contacts" "Contact" WHERE "Contact"."id" = $1',
parameters: [ '52e1da6e-f4e1-41dc-9dcd-22679c4265e4' ] }
当我查看postgres日志时,会看到以下内容:
LOG: unexpected EOF on client connection with an open transaction
为什么我要终止连接?
答案 0 :(得分:2)
单步执行代码后,我发现我在父函数之一中缺少异步等待。因此,在我的findOne查询之前调用了关闭数据库连接的afterAll块。添加缺少的等待后,此问题已解决。