title: cannot fetch data from database in javascript loop
body: <p>I'm using MySQL module in nodejs in order to read a particular column in each row to see if it is null, send another column on the same row to a web service to process the value to fill the null value. </p>
  
  <p>here is my code:</p>
  
  <pre><code>var connection = mysql.createConnection({
      host     : 'localhost',
      user     : 'root',
      password : '123456789',
      database : 'bank114'
  });
  
  var total_rows;
  
  connection.connect();
  
  connection.query('SELECT count(*) from bank114_x;', function (error, results, fileds) {
      total_rows = results[0]['count(*)'];
      console.log(total_rows);
      iterator();
  });
  
  function iterator(){
      for (var i = 1; i &lt;= total_rows; i++) {
          fetch_data(i)
  
      }
  }
  
  function fetch_data(id){
      connection.query({
              sql: 'SELECT * FROM `bank114_x` WHERE `id` = ?',
              timeout: 40000, // 40s
          },
          [id],
          function (error, results, fields) {
              console.error(error);
              if (results[0].f6781 == null){
                  //send f6780 column to a webservice to calculate f6781
              }
          }
      );
  }
  connection.end();
  </code></pre>
  
  <p>The problem is, I can do this on a single row of the table (<code>results[0]</code> returns the row), but when I use a for loop to iterate over all rows in the table, I get the error: </p>
  
  <pre><code>{ Error: Cannot enqueue Query after invoking quit.
      at Protocol._validateEnqueue (/mnt/sdb2/WebstormProjects/translator/node_modules/mysql/lib/protocol/Protocol.js:204:16)
      at Protocol._enqueue (/mnt/sdb2/WebstormProjects/translator/node_modules/mysql/lib/protocol/Protocol.js:139:13)
      at Connection.query (/mnt/sdb2/WebstormProjects/translator/node_modules/mysql/lib/Connection.js:208:25)
      at fetch_data (/mnt/sdb2/WebstormProjects/translator/main.js:29:16)
      at iterator (/mnt/sdb2/WebstormProjects/translator/main.js:23:9)
      at Query._callback (/mnt/sdb2/WebstormProjects/translator/main.js:18:5)
      at Query.Sequence.end (/mnt/sdb2/WebstormProjects/translator/node_modules/mysql/lib/protocol/sequences/Sequence.js:88:24)
      at Query._handleFinalResultPacket (/mnt/sdb2/WebstormProjects/translator/node_modules/mysql/lib/protocol/sequences/Query.js:139:8)
      at Query.EofPacket (/mnt/sdb2/WebstormProjects/translator/node_modules/mysql/lib/protocol/sequences/Query.js:123:8)
      at Protocol._parsePacket (/mnt/sdb2/WebstormProjects/translator/node_modules/mysql/lib/protocol/Protocol.js:279:23) code: 'PROTOCOL_ENQUEUE_AFTER_QUIT', fatal: false }
  /mnt/sdb2/WebstormProjects/translator/main.js:36
              if (results[0].f6780 == null){
                         ^
  
  TypeError: Cannot read property '0' of undefined
      at Query._callback (/mnt/sdb2/WebstormProjects/translator/main.js:36:24)
      at Query.Sequence.end (/mnt/sdb2/WebstormProjects/translator/node_modules/mysql/lib/protocol/sequences/Sequence.js:88:24)
      at /mnt/sdb2/WebstormProjects/translator/node_modules/mysql/lib/protocol/Protocol.js:225:14
      at _combinedTickCallback (internal/process/next_tick.js:131:7)
      at process._tickCallback (internal/process/next_tick.js:180:9)
  </code></pre>
  
  <p>o do something with the asynchronous flow of javascript. How can I fix it?</p>