亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Node.js - 類型錯誤:客戶端不是構造函數

Node.js - 類型錯誤:客戶端不是構造函數

手掌心 2023-06-09 17:48:57
我正在嘗試以下操作:  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"我是根據我在網上找到的一個片段寫下這篇文章的,似乎在我看來到處都有人在做同樣的事情。誰能告訴我我做錯了什么?
查看完整描述

3 回答

?
BIG陽

TA貢獻1859條經驗 獲得超6個贊

這是一個 JS 錯誤:

試試這個,它對我有用:
const { Client } = require('pg');
const Client = require('pg').Client;

--ES模塊:
import pg from 'pg';
const Client = pg.Client;


查看完整回答
反對 回復 2023-06-09
?
侃侃無極

TA貢獻2051條經驗 獲得超10個贊

您的代碼適用于 CommonJS。但對于 ESM,會出現此錯誤。


ESM中正確的運行方式:


import pg from 'pg'


const client = new pg.Client(config.dbConfig)


查看完整回答
反對 回復 2023-06-09
?
寶慕林4294392

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())


查看完整回答
反對 回復 2023-06-09
  • 3 回答
  • 0 關注
  • 302 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號