I
I
Ivan Ivanovich2020-07-03 09:12:46
MySQL
Ivan Ivanovich, 2020-07-03 09:12:46

Database problem?

Hello, I am working with mysql database in node js through mysql module.

There is a function to get data:

const getData = q=>{
  return new Promise((resolve,reject)=>{
    connection.query(q, (err,result)=>{
      if(err) reject(err);
      
      resolve(result);
    });
  });
};


There is no connection.end(), so after a while the application crashes, this is understandable. ( PROTOCOL_CONNECTION_LOST )

But if I do like this:

if(err) reject(err);  
resolve(result);
connection.end();


Then on the 2nd update, the application will also crash with the PROTOCOL_ENQUEUE_AFTER_QUIT error, I understand the essence of the error, but I haven’t figured out why it occurs yet.

Although in code

connection.query(q, (err,result)=>{
if(err) throw err;
        
console.log(result);
connection.end();
});


there is no error.

Please tell me where I'm making a mistake. Thanks in advance.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Robur, 2020-07-03
@Robur

That's on the 2nd update

Did you establish a new connection to the database before these 2nd update?
Well also there is a question what for to you in general connection to close after request. the application started, opened, terminated the application - closed, exited.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question