title: node.js mysql query in a for loop
body: <p>I have two queries.<br />
  The first,</p>
  <pre><code>SELECT auctions.name, wowitemdata.itemName, auctions.itemId, 
  auctions.buyout, auctions.quantity
  FROM auctions
  INNER JOIN wowitemdata ON auctions.itemId = wowitemdata.itemID;
  </code></pre>
  <p>returns data like this:</p>
  <pre><code>{
  name: 'somename',
  itemName: 'someitemname',
  itemId: '0000',
  buyout: '0001',
  quantity: '5',
  }
  </code></pre>
  <p>The 2nd query uses data from the #1 to get the count() of items cheaper than itemId. The response is to be added to #1 as a new element -&gt; 'undercut'.</p>
  <p>My function:</p>
  <pre><code>function checkUndercut(data, length){
      var Select = 'SELECT COUNT(auctions.itemId) AS cnt ';
      var From = 'From `auctions` ';
      var Where = 'WHERE auctions.itemId LIKE ? AND buyout &lt; ?';
      var sql = Select + From + Where;
      for(i = 0, len = length; i &lt; len; i++){
          var inserts = [data[i]['itemId'], data[i]['buyout']];
          var ssql = mysql.format(sql, inserts);
          data[i]['undercut'] = i;
          connection.query(ssql, function(err, rows, fields){
              if(!err){
                  console.log(&quot;check Undercut: &quot; + rows[0].cnt);
                  data[i]['undercut'] = rows[0].cnt;
              } else {
                  console.log(&quot;Error while performing Query&quot;);
              };
          });
      };
  }
  </code></pre>
  <p>Due to the asynchronous nature of the query I can't use the for loop i var to append the data.<br />
  How do I get around this?</p>
  <p><strong>Edit:
  My new problem:</strong><br />
  When I search for a name(searchobj), the returned data is one step behind.<br />
  Say I search for &quot;Tim&quot;, nothing shows up.<br />
  Say I search for &quot;Finn&quot; next, my data on &quot;Tim&quot; shows up.</p>
  <p>I suspect this is because of the way I got data out of my checkUndercut function.<br />
  I made a new top level var, appdata and after using @simple_programmers suggestion I put my new code like this:</p>
  <pre><code>function(err){
          if(err){
            //handle the error if the query throws an error
          }else{
              appdata = data;
              console.log(appdata);
            //whatever you wanna do after all the iterations are done
          }
      });
  </code></pre>
  <p>The console.log sends out the correct information so my problem lies with the get function sending the response prematurely.</p>
  <p>My get function:</p>
  <pre><code>app.get('/test',function(req, res) {
      console.log(&quot;app.get &quot;+searchobj);
      var sqlSELECT = 'SELECT auctions.name, wowitemdata.itemName, auctions.itemId, auctions.buyout, auctions.quantity ';
      var sqlFROM = 'FROM `auctions` ';
      var sqlINNER ='INNER JOIN `wowitemdata` ON auctions.itemId = wowitemdata.itemID ';
      var sqlWHERE = 'WHERE auctions.name LIKE ?';
      var sql = sqlSELECT + sqlFROM + sqlINNER + sqlWHERE;
      var inserts = [searchobj];
      var sql = mysql.format(sql, inserts);
      //console.log(&quot;Query: &quot;+sql);
      connection.query(sql, function(err, rows, fields) {
        if (!err){
          var rowLen = rows.length;
          checkUndercut(rows, rowLen);
          console.log(appdata);
          res.send(appdata);
                  } else {
      console.log('Error while performing Query.');
      };
      
      });
  }); 
  </code></pre>
  <p>My questions:</p>
  <ol>
  <li>What is the correct way of sending data out of a async function?</li>
  <li>Is there some way I can make my app.get or res.send wait til my data is retrieved before sending it?</li>
  </ol>
  <p>Edit 2: I can get it working by placing all the code inside my app.get(), but there has to be a more elegant and easier to read solution?</p>