我的 node .js应用程序能够通过npm pg模块与本地Postgres数据库协同工作.

以下是self signed certificate错误的输出:

(node:2100) UnhandledPromiseRejectionWarning: Error: self signed certificate
    at TLSSocket.onConnectSecure (_tls_wrap.js:1051:34)
    at TLSSocket.emit (events.js:189:13)
    at TLSSocket._finishInit (_tls_wrap.js:633:8)
(node:2100) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:2100) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
D:\MY\DEV\PROJECTS\AdsSubscribeBot\test.js:57
  if (err) throw err;
           ^

Error: Connection terminated unexpectedly
    at Connection.con.once (D:\MY\DEV\PROJECTS\AdsSubscribeBot\node_modules\pg\lib\client.js:264:9)
    at Object.onceWrapper (events.js:277:13)
    at Connection.emit (events.js:189:13)
    at Socket.<anonymous> (D:\MY\DEV\PROJECTS\AdsSubscribeBot\node_modules\pg\lib\connection.js:76:10)
    at Socket.emit (events.js:194:15)
    at TCP._handle.close (net.js:597:12)

重现此错误的最简单方法是try 使用示例代码连接到 node 中.来自Heroku devcenter的js:

下面是导致self signed certificate个错误的代码示例:

const connectionString = 'postgres://USERNAME:PASSWORD@HOST:PORT/DB_NAME';

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

const client = new Client({
  connectionString: connectionString,
  ssl: true
});

client.connect();

client.query('SELECT * FROM users;', (err, res) => {
  if (err) throw err;
  for (let row of res.rows) {
    console.log(JSON.stringify(row));
  }
  client.end();
});

也许有人遇到过同样的问题,并且知道如何解决它.

提前感谢您的帮助.

推荐答案

判断你的pg配置.听起来你用的是第8页,它反对

您可以在设置pg配置的地方这样做,如下所示

const client = new Client({
  connectionString: connectionString,
  ssl: { rejectUnauthorized: false }
})

Node.js相关问答推荐

如何使用MongoDB在Node.js 中向数组中添加项?

如何更改ejs中的镜像src,以防从OMDB API获取的某些镜像具有src=N/A

对于具有重叠列的组合键,在冲突&q;上没有唯一或排除约束匹配错误

即使卷已设置,Docker Nodemon 也不会热重载

如何创建具有不同对象类型的数组类型

Nest Js - 在 multer 拦截器中如何设置最大文件上传大小并进行可选文件上传

我收到警告:发现函数rs-ms-v1不受支持的运行时nodejs18.x× 不受支持的运行时

无服务器部署使用无服务器组合抛出`spawn serverless ENOENT`

在 React 和 Socket.io 中使用 Effect 钩子重新渲染两次

多个 Axios 请求合并

如何更新 MongoDB 中对象数组中的键?

Sharp JS 依赖关系 destruct 了 Elastic Beanstalk 上的 Express Server

如果 express.js (node.js) http 请求在完成之前关闭会发生什么?

响应发送 200 而不是 403

使用restify时如何支持cors

使用 ES6 语法和动态路径导入模块

如何让should.be.false语法通过 jslint?

Heroku + Node:找不到模块错误

node.js 找不到模块mongodb

`return function *(){...}` 是什么意思?