最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Node.js - TypeError: Client is not a constructor - Stack Overflow

programmeradmin2浏览0评论

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 like pgClient 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
Add a ment  | 

3 Answers 3

Reset to default 12

This 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())
发布评论

评论列表(0)

  1. 暂无评论