【问题标题】:node-postgres: [error] This socket has been ended by the other partynode-postgres: [错误] 此套接字已被对方结束
【发布时间】:2017-09-14 09:33:03
【问题描述】:

我使用 node-protgres 来操作我的 nodejs 应用程序中的数据库。

我做了什么:

const { Pool, Client } = require('pg')
var dbconnect = {
    user: 'xxxxx',
    database: 'xxxxx',
    password: 'xxxxx', 
    host: 'xxxxx.yyyyy.zzzzz.eu-west-1.rds.amazonaws.com',
    port: 0000, 
    max: 20, // max number of clients in the pool
    idleTimeoutMillis: 30000,
    connectionTimeoutMillis: 2000 
};
const pool = new Pool(dbconnect);
pool.on('error', function (err, client) {
    console.error('idle client error', err.message, err.stack)
});

function listOfPets(req, res) {

 pool.connect(function (err, client, done) {
        if (err) {
            return console.error('error fetching client from pool', err);
        }

        var sql =
            "SELECT * FROM pets"
        client.query(sql, function (err, result) {

                done();

                if (err) {
                    return console.error('error running query', err);
                }

                ... //Handle the result 

            });
    });

}

该功能工作正常,但服务器不断向我发送错误报告,从正常到严重。我检查了日志:

idle client error 此套接字已被对方终止错误: 此套接字已被对方结束 在 Socket.writeAfterFIN [as write] (net.js:291:12) 在 Connection.end (/var/app/current/node_modules/pg/lib/connection.js:313:22) 在 global.Promise (/var/app/current/node_modules/pg/lib/client.js:410:23) 在 Client.end (/var/app/current/node_modules/pg/lib/client.js:409:12) 在 Pool._remove (/var/app/current/node_modules/pg-pool/index.js:135:12) 在 Timeout.setTimeout (/var/app/current/node_modules/pg-pool/index.js:38:12) 在 ontimeout (timers.js:365:14) 在 tryOnTimeout (timers.js:237:5) 在 Timer.listOnTimeout (timers.js:207:5)

我认为问题来自'done()' 不起作用或放置在错误的位置。

感谢任何建议。

【问题讨论】:

    标签: javascript node.js postgresql node-postgres


    【解决方案1】:

    尝试在回调中声明pool 对象。我在 postgres 客户端上遇到了类似的错误。我通过在 GET 请求的回调中声明 client 来解决它。

    看看这个问题,这是我找到解决方案的地方:Github issue

    【讨论】:

      【解决方案2】:

      希望这可以帮助你 =)。我想你可以使用这个链接来修复http://mherman.org/blog/2015/02/12/postgresql-and-nodejs/#.WbpNjMgjGHs

      【讨论】:

      • 您好,感谢您分享这篇文章。本教程不使用 pool 并导致使用 pg-promise 而不是 node-postgres。你建议我用哪一个?顺便说一句,这是一篇有用的文章
      猜你喜欢
      • 1970-01-01
      • 2012-03-31
      • 2016-06-19
      • 1970-01-01
      • 2016-04-19
      • 1970-01-01
      • 2016-04-19
      • 2020-05-02
      • 2021-10-26
      相关资源
      最近更新 更多