Here is my code that performs multiple select queries one after other.
const {Pool} = require('pg');
const pool = new Pool(POSTGRES_CONFIG);
var aggregateDomainCount = [], domainCount={};
pool.connect((err, client, release) => {
if (err) {
return console.error('Connection error', err.stack)
}
console.log('Connection established');
client.query(firstQuery, function(err, results) {
// call `done()` to release the client back to the pool
if(err) {
return console.error('error running query', err);
}
domainCount = {"data1":results.rows[0].count};
aggregateDomainCount.push(domainCount);
});
......
client.query(lastQuery, function(err, results) {
// call `done()` to release the client back to the pool
if(err) {
return console.error('error running query', err);
}
domainCount = {"dataN":results.rows[0].count};
aggregateDomainCount.push(domainCount);
release();
response.json(aggregateDomainCount);
});
});
The issue is while executing above queries, console first prints('Connection established') and then after series of execution, again it tries to enter pool.connect before completing all queries and prints('Connection established') and finally results in unknown error to user interface. If i reduce number of queries by some, then I get expected results but which is again inconsistent.
So far, I have tried
- nesting client.query one inside other
- nesting pool.connect by grouping set of queries
- setting different timeout parameters
- using await client.query()
But all the above resulted in same issue. Could you please point me in right direction.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…