4

try/catch does not work for client.connect():

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

Error:

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

How can I change the async connect() to sync for try/catch?

Mureinik
  • 297,002
  • 52
  • 306
  • 350
Henry
  • 1,077
  • 1
  • 16
  • 41

1 Answers1

6

connect() without a callback function returns a promise, which you could apply the .catch method to:

client
  .connect()
  .then(() => console.log('connected'))
  .catch(err => console.error('connection error', err.stack))
Mureinik
  • 297,002
  • 52
  • 306
  • 350