我安装了"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');
}
connect
返回一个Promise
,然后转到下一个语句。相反,您应该使用then
和cath
方法:
pool.connect()
.then(c => console.log('connected to the db'))
.catch(e => console.log('Error connecting to db'));