Node.js Postgres服务器未响应nodejs请求
我可以从pgAdmin4访问远程postgres DB,也可以使用Mac从NodeJ访问。现在我正在使用相同的代码访问Windows中的DB。我的连接代码如下所示:Node.js Postgres服务器未响应nodejs请求,node.js,postgresql,node-postgres,Node.js,Postgresql,Node Postgres,我可以从pgAdmin4访问远程postgres DB,也可以使用Mac从NodeJ访问。现在我正在使用相同的代码访问Windows中的DB。我的连接代码如下所示: const { Client } = require('pg'); //Importing the Postgres package const hosts= require('../hosts'); //Using the file containig all hosts const connectionData = { //B
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
})
我的测试如下:
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
})
没有错误,没有异常,数据库服务器没有响应
为什么服务器没有响应?我怀疑您也有类似的问题。由于它不是100%重复,我将再次发布: pg模块和NodeJS 14中有一个 建议的解决方案是确保已安装
pg>=8.0.3
这可以通过更新依赖项中的pg
来完成
另外,还要确保依赖于pg
模块的任何其他库也是最新的,并且具有最新的pg
版本
如果由于任何原因无法做到这一点,那么使用Node 12也应该可以。您使用的是Node.JS 14吗?另外:发布您的
包.json
-If any.Nodejs v14.1.0 npm v6.14.4`{“name”:“myServer”,“version”:“0.0.0”,“private”:true,“scripts”:{“start”:“nodemon bin/www”},“dependencies”:{“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错误”:“~1.6.3”,“jade”:“~1.11.0”,“morgan”:“~1.9.1”,“nodemon”:“^2.0.3”,“pg”:“^8.0.2”,“sha.js”:“^2.4.11”}非常感谢!非常有用,谢谢!非常感谢。我花了太长时间才找到这个。