如何在 node-postgres 中捕获 client.connect()?

How to catch for client.connect() in node-postgres?

try/catch 不适用于 client.connect():

const { Pool, Client } = require('pg')
try {
  const client = new Client(config)
  client.connect()
} catch (er) {
  console.log('error')
}

错误:

UnhandledPromiseRejectionWarning: error: password authentication failed for user '...'

如何将异步 connect() 更改为同步 try/catch?

connect() 没有回调函数 returns 承诺,您可以将 .catch 方法应用于:

client
  .connect()
  .then(() => console.log('connected'))
  .catch(err => console.error('connection error', err.stack))