6

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?

1
  • 1
    await it in an async function, or use .then and .catch instead Commented Dec 29, 2019 at 10:22

1 Answer 1

7

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))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.