node.js - 为什么当我调用 client.connect() 时 node-postgres (pg) 挂起?

标签 node.js postgresql freeze node-postgres

我最近不得不将我的 vue.js 应用程序(后端的 node.js)的 node.js 版本从 v13.5.0 升级到 v14.5.0。我重新安装了所有 Node 包,升级了我必须升级的那些,现在应用程序卡在所有数据库调用上。我们使用 pg (node-postgres) 来调用我们的数据库。我将 pg 升级到 7.18.2 版。
我们的初始化代码如下所示:

constructor() {
  this.pg = require('pg');
  this.client = null;
  this.initPromise = null;
}

async init() {
  if (!this.initPromise) {
    this.client = new this.pg.Client({
      application_name: 'Back end',
      ssl: {
        rejectUnauthorized: false
      }
    });
    this.initPromise = this.client.connect();
  }
  return this.initPromise;
}

async query(query, params) {
  await this.init();
  return await this.client.query(query, params);
}
我将控制台日志放在对 this.init() 的调用中,如下所示:
console.log('before');
await this.init();
console.log('after');
'after' 永远不会打印出来。
有谁知道为什么我升级了 Node 版本后它会挂起?

最佳答案

看来 pg v.8.3.0 解决了这个问题。我通过运行 npm install pg 获得了 v7.18.2。似乎并不总是安装最新版本。 npm install pg@latest 可以解决问题。

关于node.js - 为什么当我调用 client.connect() 时 node-postgres (pg) 挂起?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62876107/

相关文章:

c# - 为什么这一行代码会导致 Visual Studio 崩溃?

python - 了解分发卡住的 Python Tkinter 应用程序需要 TCL 中的哪些文件

javascript - 在每次迭代期间迭代和进行异步调用的最佳方法

postgresql - 在 PostgreSQL 9.2 中基于循环创建动态表

arrays - 如何使用 PostgreSQL 更新 JSON 数组

Python 多线程与 Postgresql

javascript - 在 Node 中进行导入时避免相对路径

javascript - Node 套接字挂断错误与http

javascript - 将ejs转换为jade?

制作exe文件时VB6卡住