node-postgres: Connection terminated error with pooling

Hi everybody !

I have a little problem with my code. I’m trying to request my DB to get the pseudo linked with a specific ID. I’m using the pool as recommended with the documentation but I have an error. I think I’m missing something but I don’t know why.

This is my code:

function pseudo_utilisateur(id){
    return new Promise(function(resolve, reject){
        let query = `SELECT pseudo FROM ${Tables.user} WHERE id = $1`;
        pool.connect(function(err, client, done){
            client.query(query, [id],function(err, result){
                done();
                if(err){
                    console.log(err);
                    reject("err");
                }
                else{
                    console.log(result);
                    resolve(result.rows[0].pseudo);
                }
            })
        })
    })
}

and this is the error I received:

Error: Connection terminated
    at Connection.<anonymous> (E:\Cours année 2015 - 2017\Mémoire\mookup\node_modules\pg\lib\client.js:198:29)
    at Object.onceWrapper (events.js:291:19)
    at emitNone (events.js:86:13)
    at Connection.emit (events.js:186:7)
    at Socket.<anonymous> (E:\Cours année 2015 - 2017\Mémoire\mookup\node_modules\pg\lib\connection.js:78:10)
    at emitOne (events.js:96:13)
    at Socket.emit (events.js:189:7)
    at TCP._handle.close [as _onclose] (net.js:501:12)

I have read this is a common error when someone uses the client approach. But I don’t think I close the connection before reading the results. If I have well understood, the done() method is to release the client and the result of the request is in the callback of the client. But maybe I’m wrong.

So can someone help me, please ? Thank you very much

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 16 (3 by maintainers)

Most upvoted comments

@JulesAU @AkhilGopan @baninaveen again, please open a new issue with reproduction code and the error you see. It doesn’t help to write on a closed issue, which the OP has closed because he has solved his own bug.