node.js - 异常处理不适用于 PG npm 包
问题描述
我使用数据库凭据安装"pg": "^8.0.2"
并创建了该文件。database.js
但无论出现什么问题,它都不会进入catch block
显示错误。相反,它总是记录connected to the 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');
}
解决方案
connect
返回 a Promise
,然后转到下一条语句。相反,您应该使用then
andcath
方法:
pool.connect()
.then(c => console.log('connected to the db'))
.catch(e => console.log('Error connecting to db'));
推荐阅读
- android - 为什么Android和iOS在应用关闭时不杀死进程?
- java - 我可以在 SQL 中自动更新当前日期吗?
- apache - 使用 .htaccess 重命名动态 URL?
- python - 你能检测到客户端跟上 websocket 消息流的速度吗?
- angular7 - Angular 7设置选定值不触发更改事件
- regex - 高级正则表达式:这种模式的正则表达式是什么?
- json - 使用 splice() 从数组中删除匹配条件的项目
- c++ - 在我的线程/原子值代码中找不到错误
- php - 仅 php 上的远程连接错误,而不是使用 php_pdo 窗口的 mysql 8 上的 Navicat/workbench
- python - 1D CNN 是否需要填充以保留输入长度?