title: Node JS MySQL query dependent on another query
body: <p>In the following code, I have a select query called <code>queryString</code> which retrieves information about some tweets that are already stored in the database. I want to retrieved the hashtags that are mentioned in those tweets so that I show to a user the tweet information along with the its hashtags:</p>
  
  <pre><code>var queryString = 'select Tweet.Label, Tweet.TDate, Tweet.TLink, Author.Lable, Author.ALink from Tweet, Author where Tweet.AuthorID IN (select ID from Author where Lable = ?) AND Author.ID IN (select ID from Author where Lable = ?)';
  var query = connection.query(queryString, [term,term], function(err, rows) {
          console.log(rows);
          //res.write(JSON.stringify(rows));
          var tweet = JSON.parse(JSON.stringify(rows));
          for(var i in tweet){
              res.write("Author: ");
              res.write("&lt;a href='" + tweet[i].ALink + "' target='_blank'&gt;" + tweet[i].Lable + "&lt;/a&gt; &lt;br/&gt;");
              res.write("Date: " + tweet[i].TDate  + "&lt;br/&gt;");
              res.write("Tweet: " + "&lt;a href='" + tweet[i].TLink + "' target='_blank'&gt;" + tweet[i].Label + "&lt;/a&gt; &lt;br/&gt;");
              var query1 = connection.query('select Label from Hashtag where ID IN (select HashID from tweethashs where TweetID IN (select ID from Tweet where Label = ?))', [tweet[i].Label], function(err, rows1) {
                      var tweet1 = JSON.parse(JSON.stringify(rows1));
                      for(var i in tweet1){
                          res.write("Hashtag: ");
                          res.write(tweet1[i].Label);
                      }
                  }
              );
              res.write("&lt;br/&gt;&lt;br/&gt;");
          }
  
          res.end();
      });
  </code></pre>
  
  <p>What I did is I included a query for the hashtags inside the loop of tweets so that I take the tweet as a parameter for the hashtags query (in where clause). When I run the code, I got the following error:</p>
  
  <pre><code>events.js:154
        throw er; // Unhandled 'error' event
        ^
  
  Error: write after end
      at ServerResponse.OutgoingMessage.write (_http_outgoing.js:426:15)
      at Query._callback (C:\Users\Nasser\Desktop\Spring Semester\Intelligent Web\
  Node JS\SocialSearch.js:58:11)
      at Query.Sequence.end (C:\Users\Nasser\Desktop\Spring Semester\Intelligent W
  eb\Node JS\node_modules\mysql\lib\protocol\sequences\Sequence.js:96:24)
      at Query._handleFinalResultPacket (C:\Users\Nasser\Desktop\Spring Semester\I
  ntelligent Web\Node JS\node_modules\mysql\lib\protocol\sequences\Query.js:144:8)
  
      at Query.EofPacket (C:\Users\Nasser\Desktop\Spring Semester\Intelligent Web\
  Node JS\node_modules\mysql\lib\protocol\sequences\Query.js:128:8)
      at Protocol._parsePacket (C:\Users\Nasser\Desktop\Spring Semester\Intelligen
  t Web\Node JS\node_modules\mysql\lib\protocol\Protocol.js:280:23)
      at Parser.write (C:\Users\Nasser\Desktop\Spring Semester\Intelligent Web\Nod
  e JS\node_modules\mysql\lib\protocol\Parser.js:73:12)
      at Protocol.write (C:\Users\Nasser\Desktop\Spring Semester\Intelligent Web\N
  ode JS\node_modules\mysql\lib\protocol\Protocol.js:39:16)
      at Socket.&lt;anonymous&gt; (C:\Users\Nasser\Desktop\Spring Semester\Intelligent W
  eb\Node JS\node_modules\mysql\lib\Connection.js:96:28)
      at emitOne (events.js:90:13)
  </code></pre>
  
  <p>Can someone help me solving this problem</p>