【问题标题】:Postgres server is not responding to a nodejs requestPostgres 服务器没有响应 nodejs 请求
【发布时间】:2020-08-19 23:45:07
【问题描述】:

我可以从 pgAdmin4 访问远程 postgres 数据库,也可以使用 Mac 从 nodejs 访问。现在我正在使用相同的代码来访问 Windows 中的数据库。我的连接代码如下:

const { Client } = require('pg'); //Importing the Postgres package
const hosts= require('../hosts'); //Using the file containig all hosts 
const connectionData = { //Begin creating the connection settings object
   host: hosts.DBHost, //DB host   
   port: hosts.DBPort, //DB hosts port
   database: hosts.DB, //DB
   user: hosts.DBUser, //DB user
   password: hosts.DBPassword, //DB user password
 } 

我的测试如下:

var client = new Client(connectionData); //New client instance using the above connection settings
client.connect(); //Open the connection to the database()  
sql = "select * from myTable";
client.query(sql) 
  .then(response => {
    console.log ({"data": response}); //This isn't shown 
  })
  .catch(err => { 
    console.log({"error": err}); //This isn't shown neither 
  })

没有错误,没有异常,DB服务器没有响应!

为什么服务器没有响应?

【问题讨论】:

  • 你使用的是 Node.JS 14 吗?
  • 另外:发布您的package.json - 如果有的话。
  • Nodejs v14.1.0 npm v6.14.4
  • ` { "name": "myServer", "version": "0.0.0", "private": true, "scripts": { "start": "nodemon ./bin/www " }, "依赖项": { "cookie-parser": "~1.4.4", "cors": "^2.8.5", "crypto": "^1.0.1", "csprng": "^0.1 .2", "debug": "~2.6.9", "express": "~4.16.1", "http-errors": "~1.6.3", "jade": "~1.11.0", “摩根”:“~1.9.1”,“nodemon”:“^2.0.3”,“pg”:“^8.0.2”,“sha.js”:“^2.4.11”}}`跨度>

标签: node.js postgresql node-postgres


【解决方案1】:

我怀疑您遇到了与other post 相同的问题。由于它不是 100% 重复,我将再次发布:

pg 模块和 NodeJS 14 中有一个known issue。

建议的解决方案是确保您已安装 pg>=8.0.3。

这可以通过更新依赖项中的pg 来完成。

还要确保依赖于pg 模块的任何其他库也是最新的并且具有最新的pg 版本。

如果出于任何原因无法做到这一点 - 使用 Node 12 也应该可以。

【讨论】:

  • 非常感谢!
  • 谢谢!我花了很长时间才找到这个。
猜你喜欢
  • 2015-10-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-23
  • 1970-01-01
  • 2018-04-24
相关资源
最近更新 更多