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

gib*_*b65 2 postgresql freeze node.js node-postgres

我最近不得不将我的 vue.js 应用程序(后端的 node.js)的 node.js 版本从 v13.5.0 升级到 v14.5.0。我重新安装了所有节点包,升级了我必须升级的那些,现在应用程序挂在所有数据库调用上。我们使用 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);
}
Run Code Online (Sandbox Code Playgroud)

我将控制台日志放在对 this.init() 的调用中,如下所示:

console.log('before');
await this.init();
console.log('after');
Run Code Online (Sandbox Code Playgroud)

'after' 永远不会打印出来。

有谁知道为什么我升级了节点版本后它会挂起?

gib*_*b65 9

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