我使用node-protgres來操作我的nodejs應用程序中的數據庫。node-postgres:[error]該套接字已被對方終止
我做了什麼:
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
});
});
}
功能工作正常,但是服務器繼續向我發送錯誤報告OK嚴重。我檢查了日誌:
idle client error This socket has been ended by the other party Error: This socket has been ended by the other party at Socket.writeAfterFIN [as write] (net.js:291:12) at Connection.end (/var/app/current/node_modules/pg/lib/connection.js:313:22) at global.Promise (/var/app/current/node_modules/pg/lib/client.js:410:23) at Client.end (/var/app/current/node_modules/pg/lib/client.js:409:12) at Pool._remove (/var/app/current/node_modules/pg-pool/index.js:135:12) at Timeout.setTimeout (/var/app/current/node_modules/pg-pool/index.js:38:12) at ontimeout (timers.js:365:14) at tryOnTimeout (timers.js:237:5) at Timer.listOnTimeout (timers.js:207:5)
我覺得問題來自'done()'不起作用或被放在錯誤的地方。
任何建議表示讚賞。
嗨,感謝您分享這篇文章。本教程不會使用池並導致使用pg-promise而不是node-postgres。你建議我使用哪一個?順便說一句,這是一篇有用的文章 –