A
A
Archakov Dennis2016-11-30 02:49:50
MySQL
Archakov Dennis, 2016-11-30 02:49:50

NodeJS + MySQL: Cannot enqueue Query after invoking quit.?

var mysql = require('mysql');

class DB {

  constructor(){

    this.connection = mysql.createConnection({
      host     : 'localhost',
      user     : 'root',
      password : '123',
      database : 'test',
      socketPath: '/Applications/MAMP/tmp/mysql/mysql.sock'
    });

    this.connection.config.queryFormat = (query, values) => {
      if (!values) return query;
        return query.replace(/\:(\w+)/g, (txt, key) => {
        if (values.hasOwnProperty(key)) {
          return mysql.escape(values[key]);
        }
        return txt;
      });
    };

    this.connection.connect();

  }

  query(q, params, callback){
    var _this = this;
    this.connection.query(q, params, (err, rows, fields) => {
      _this.connection.end();
      callback(err, rows, fields);
    });
  }

}

module.exports = DB;

On the first request, everything is ok. But, when the second request is sent to the database, then the error is:
Error: Cannot enqueue Query after invoking quit.
I can’t understand what the problem is, I’m calling _this.connection.end();.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladlen Hellsite, 2016-11-30
@archakov06

Why such cumbersome constructions, maybe you should use KnexJS ?
Yes, and you are not creating a poll, and this is not very good.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question