【问题标题】:While trying to start the server, system throws Client is not a constructor error尝试启动服务器时,系统抛出 Client is not a constructor 错误
【发布时间】:2023-02-14 17:21:17
【问题描述】:

从命令行通过node server.js启动服务器时,系统抛出以下错误:

const pool = new Client({ ^ TypeError: Client is not a constructor at Object.<anonymous> (C:\Max\study\blogs\src\db\dbConfig.js:3:14) 有人可以建议如何解决此错误吗?

数据库/dbConfig.js

const Client = require("pg");

const pool = new Client({
  host: "localhost",
  user: "postgres",
  port: 5432,
  password: "some_password",
  database: "blogs",
});


pool.connect(function(err) {
    if (err) throw err;
});

module.exports = pool;

源代码/server.js

const express = require("express");
const db = require("./db/dbConfig");

const bodyParser = require("body-parser");
var multer  = require('multer')
var path = require('path');
const moment = require('moment');

const app = express();

  
app.listen(port, () => {
  console.log(`Example app listening on port ${port}`)
})

【问题讨论】:

    标签: node.js postgresql


    【解决方案1】:

    您使用了错误的导入语句。代替

    const Client = require("pg");
    

    你应该使用

    const { Client } = require("pg");
    

    这是 db/dbConfig.js 的更正代码:

    const { Client } = require("pg");
    
    const pool = new Client({
      host: "localhost",
      user: "postgres",
      port: 5432,
      password: "some_password",
      database: "blogs",
    });
    
    pool.connect(function(err) {
        if (err) throw err;
    });
    
    module.exports = pool;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-21
      • 1970-01-01
      • 2018-07-25
      • 1970-01-01
      • 2011-03-07
      相关资源
      最近更新 更多