Re-use method

0

I made a query to search for a user. It worked!

But I tried to make another query, he gave this message

Error: Cannot use a pool after calling end on the pool 

Because every time I make a connection, I close it later, and it seems that once it is closed it does not reopen.

I'm using nodejs 10 with postgres

This is my method:

       return new Promise( (resolve, reject) => {
                const { pool, errorHandler } = deps
                const query = "SELECT * FROM public.usuario  WHERE cpf = $1"
                const queryData = [ cpf ]
                pool.connect()
                pool.query( query, queryData, (error, results) => {

                    if(error){
                        errorHandler(error, 'Não foi possível encontrar usuário com cpf ${cpf}', reject)
                        return false
                    }
                    if( results.rowCount > 0 ){
                        resolve({ usuario: results.rows })
                    }else{
                        resolve({msg: 'Usuário não encontrado'})
                    }

                })
                pool.end()
            })

Notice that every time I have access, I pass the connect() method, but it is not working

And you still have this message:

Error: Called end on pool more than once
    
asked by anonymous 05.08.2018 / 17:49

1 answer

0

I was able to resolve by changing the code to

return new Promise( (resolve, reject) => {
                const { pool, errorHandler } = deps
                pool.connect((err, client, done) => {
                    if( err ){
                        errorHandler(err, 'Problema ao conectar ao banco', reject)
                        return false
                    }
                    const query = "SELECT * FROM public.usuario  WHERE cpf = $1"
                    const queryData = [ cpf ]
                    client.query( query, queryData, (error, results) => {
                        done()
                        if(error){
                            errorHandler(error, 'Problema ao encontrar usuário com cpf ${cpf}', reject)
                            return false
                        }
                        if( results.rowCount > 0 ){
                            resolve({ usuario: results.rows[0], msg: '', status: true })
                        }else{
                            resolve({msg: 'Usuário não encontrado', status: false})
                        }
                    } )
                })

According to the documentation

    
05.08.2018 / 18:29