【问题标题】:How to catch for client.connect() in node-postgres?如何在 node-postgres 中捕获 client.connect()?
【发布时间】:2019-12-29 10:19:37
【问题描述】:

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?

【问题讨论】:

  • 在异步函数中等待它,或者使用 .then 和 .catch 代替

标签: node.js postgresql error-handling node-postgres


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2020-11-02
    • 2019-02-08
    • 2021-03-21
    • 2012-06-17
    • 1970-01-01
    • 2013-03-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多