【发布时间】: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