D
D
Di Lee2015-12-21 01:21:41
MySQL
Di Lee, 2015-12-21 01:21:41

Node.js + mysql nested queries, where is the error?

Tell me what's wrong? I can't figure out if the code seems to be correct, but the second request doesn't work.
throws an error: Cannot enqueue Query after invoking quit

var mysql = require('mysql');
var connection = mysql.createConnection({
        host     : 'localhost',
        user     : 'root',
        password : '',
        database : 'vchat'
    });
  
  connection.query('SELECT * from therooms', function(err, rows, fields) {
    if (!err){
       connection.query('SELECT roomid from therooms', function(errw, rowsw, fieldsw) {
         if (!errw){
           console.log('INNER RESULT: '+rowsw);
         }else{
          console.log('2Error while performing Query.'+errw);
        }
      });
    }else{
      console.log('1Error while performing Query.');
    }
  });

This error comes from the second query:
2Error while performing Query.Error: Cannot enqueue Query after invoking quit

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Di Lee, 2015-12-22
@denlem

The solution is:
It was necessary to remove connection.end() after the first connection.query().
The connection was closed and the second asynchronous call was triggered after the connection was closed.
Therefore, the connection must either be closed correctly (inside connection.query), or not closed at all if the script is running all the time

A
Alexander Litvinenko, 2015-12-21
@edli007

try
https://github.com/felixge/node-mysql#pooling-conn...
But why do you need a muscle on a node anyway? all these requests .. not too lazy to write them? simpler than monga, where the same JS is used

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question