我正在尝试以下操作:
const { Client } = require('pg');
console.log(Client);
const client = new Client({
user: 'Username censored',
host: 'Host censored',
database: 'gisuebung',
password: 'Passworded censored',
port: 5432,
});
client.connect();
但是,当我运行此命令时,出现以下错误:
Error in v-on handler: "TypeError: Client is not a constructor"
我是在网上找到的一段代码后写下这篇文章的,似乎到处都有人做了完全相同的事情。谁能告诉我我做错了什么?
这是一个 JS 错误:
试试这个,它对我有用:
const { Client } = require('pg');
// or
const Client = require('pg').Client;
-- ES 模块:
import pg from 'pg';
const Client = pg.Client;
您的代码适合 CommonJS。 但对于 ESM 会出现此错误。
ESM正确运行方式:
import pg from 'pg'
const client = new pg.Client(config.dbConfig)
试试这个,它对我有用:
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())