【问题标题】:Node.js - TypeError: Client is not a constructorNode.js - TypeError:客户端不是构造函数
【发布时间】:2021-02-06 18:14:22
【问题描述】:

我正在尝试以下方法:

  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"

我在网上找到了一个 sn-p 后写了这篇文章,似乎在我看到的所有地方人们都在做完全相同的事情。谁能告诉我我做错了什么?

【问题讨论】:

  • 信息不足。不过,它可能是许多不同的事情,最有可能是版本问题,即 pg 依赖关系。查看您使用的版本,因为这似乎是 2.0 版的常见问题 ~
  • 它对我来说效果很好,所以请尝试检查您的依赖项或将您的 const 名称从 client 更改为 pgClient 以检查解构是否不是问题
  • 也为我工作!
  • v-on handler”指的是什么?
  • 我正在使用 Vue.js,所以 v-on 处理程序来自那里

标签: javascript node.js postgresql


【解决方案1】:

这是一个 JS 错误:

试试这个,它对我有用:
const { Client } = require('pg');
const Client = require('pg').Client;

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

【讨论】:

    猜你喜欢
    • 2018-08-22
    • 1970-01-01
    • 2017-08-02
    • 2017-04-08
    • 2021-06-08
    • 2019-08-14
    • 1970-01-01
    相关资源
    最近更新 更多