【问题标题】:Exception handling not working for PG npm package异常处理不适用于 PG npm 包
【发布时间】:2020-04-15 14:23:58
【问题描述】:

我安装了"pg": "^8.0.2" 并使用数据库凭据创建了database.js 文件。但无论出现什么问题,它都不会进入catch block 以显示错误。相反,它总是记录connected to the database。谁能指出我做错了什么。谢谢!

Database.js

const Pool = require('pg').Pool;

  const pool = new Pool({
    user: 'roothjk',
    host: 'localhost',
    database: 'sf',
    password: 'admin',
    port: 5432
  });
  try {
    pool.connect()
    console.log('connected to the db');

  } catch (e) {
    console.log('Error connecting to db');

  }

【问题讨论】:

    标签: node.js postgresql express


    【解决方案1】:

    connect 返回一个Promise,然后您转到下一条语句。相反,您应该使用thencath 方法

    pool.connect()
        .then(c => console.log('connected to the db'))
        .catch(e => console.log('Error connecting to db'));
    

    【讨论】:

      猜你喜欢
      • 2017-07-15
      • 1970-01-01
      • 2011-05-08
      • 1970-01-01
      • 1970-01-01
      • 2016-04-20
      • 2017-08-23
      • 1970-01-01
      • 2020-11-17
      相关资源
      最近更新 更多