3 回答

TA贡献1859条经验 获得超6个赞
这是一个 JS 错误:
试试这个,它对我有用:const { Client } = require('pg');
const Client = require('pg').Client;
--ES模块:import pg from 'pg';
const Client = pg.Client;

TA贡献2051条经验 获得超10个赞
您的代码适用于 CommonJS。但对于 ESM,会出现此错误。
ESM中正确的运行方式:
import pg from 'pg'
const client = new pg.Client(config.dbConfig)

TA贡献2021条经验 获得超8个赞
试试这个,它对我有用:
const { Client } = require('pg');
const client = new Client({
user: "postgres",
database: "databasename",
port: 5432,
host: "localhost",
password: "yourpassword",
ssl: false
});
client.connect();
client.query("select * from cad_client")
.then(results => {
const result = results.rows
console.log(result)
})
.finally(() => client.end())
添加回答
举报