Command Destroy in Javascript

Asked

Viewed 30 times

0

I am starting my studies in JS and came across a problem generated by the use of the function.

I am using knex and Node.js to connect to a Mysql database. For this I used as an example the tutorial of the following link: https://zetcode.com/javascript/knex/

Well, here’s my code:

const options = {
    client: 'mysql2',
    connection: {
        host: 'localhost',
        database: 'teste',
        user: 'teste',
        password: 'teste@teste'
    }
}

const knex = require('knex')(options);

const getTasks = () => {
    knex.from('clientes').select("*")
        .then(rows => {
            const data = rows.map(row => {
                return {
                    id: row.id,
                    nome: row.nome,
                }
            })
            console.log(data)
        })
        .catch((err) => { console.log(err) })
        .finally(() => {
            knex.destroy();
        })
}

getTasks()

Selection of the list is done, but always returns the following error:

node:events:355
      throw er; // Unhandled 'error' event
      ^

Error: read ECONNRESET
    at TCP.onStreamRead (node:internal/stream_base_commons:211:20)
Emitted 'error' event on Connection instance at:
    at Connection._notifyError (C:\Users\Bruno\Documents\js\curso-react-native\apagar\node_modules\mysql2\lib\connection.js:225:12)
    at Connection._handleFatalError (C:\Users\Bruno\Documents\js\curso-react-native\apagar\node_modules\mysql2\lib\connection.js:156:10)
    at Connection._handleNetworkError (C:\Users\Bruno\Documents\js\curso-react-native\apagar\node_modules\mysql2\lib\connection.js:169:10)
    at Socket.emit (node:events:378:20)
    at emitErrorNT (node:internal/streams/destroy:188:8)
    at emitErrorCloseNT (node:internal/streams/destroy:153:3)
    at processTicksAndRejections (node:internal/process/task_queues:81:21) {
  errno: -4077,
  code: 'ECONNRESET',
  syscall: 'read',
  fatal: true
}

When I take the part of Finally containing the error Destroy() add up.

Can someone explain to me why???

Thank you for your attention

1 answer

0

I think the Destroy method is asynchronous. You have to start the method to finish. Try to change the code to:

//...
.finally(async () => await knex.destroy())
  • Hi friend. I changed, but the error persists!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.