I am trying the following:
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();
When I run this however, I get the following Error: Error in v-on handler: "TypeError: Client is not a constructor"
I wrote this after a snippet I found online and it seems everywhere I look people did the exact same thing. Can anyone tell me what I am doing wrong?
I am trying the following:
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();
When I run this however, I get the following Error: Error in v-on handler: "TypeError: Client is not a constructor"
I wrote this after a snippet I found online and it seems everywhere I look people did the exact same thing. Can anyone tell me what I am doing wrong?
Share Improve this question asked Oct 23, 2020 at 16:43 MikeHMikeH 431 gold badge2 silver badges5 bronze badges 5- Not enough informaiton. It can be a number of different things though, the likeliest would be a version issue i.e., pg dependency. Look into the version your using, as this seems to be a mon issue version 2.0 ~ – TheoNeUpKID Commented Oct 23, 2020 at 17:03
-
1
It worked well for me so try to check your dependencies or change your const name from
client
to something likepgClient
to check if is not an issue with the destructuring – RamiroP Commented Oct 23, 2020 at 17:11 - worked for me too! – MWO Commented Oct 23, 2020 at 17:35
- What does "v-on handler" refer to? – Bergi Commented Oct 23, 2020 at 18:42
- I am working with Vue.js, so v-on handler es from there – MikeH Commented Oct 23, 2020 at 19:56
3 Answers
Reset to default 12This is a JS error:
Try this, it worked for me:
const { Client } = require('pg');
// or
const Client = require('pg').Client;
-- ES module:
import pg from 'pg';
const Client = pg.Client;
Your code fine for CommonJS. But for ESM this error will raise.
Correct way to run in ESM:
import pg from 'pg'
const client = new pg.Client(config.dbConfig)
Try this, it worked for me:
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())