title: node js mysql insert query making variable value undefined
body: <p>I have the following piece of code which extracts data from a web url, displays it to the user as well as updates a mysql database - which works fine except for one small issue:</p>
  
  <pre><code>function RunScrapingIndexWatch() {
      myhttp.get('http://www.url.com',
              function (_html) {
          if (_html &amp;&amp; _html.length &gt; 10) {
              news.indexwatch = [];
              $ = cheerio.load(_html);
              status = false;
                  $('td.text21').each(function () {
                      status = true;
                      var price1 = parseFloat($(this).text().substr($(this).text().indexOf("Nifty :") + 7, 7).trim());
                      var abc = stringHelper.getIndiaTime();
                      var IWTime = abc.toLocaleTimeString();
                      news.indexwatch.push({
                          'price1': price1,
                          'IWTime': IWTime,
                          });
  
  
                  var post = {
                          'price1': price1,
                          'IWTime': IWTime
                          };
  
  
  
  
              connection.query('INSERT INTO IW SET ?', post, function (err,result){
              if (err) 
              {console.log("IW sql insert error : " +price1 +IWTime);}
              else {
                  console.log("IW data inserted : " +price1 +" | " +IWTime);
              }
          });
  
  
  
                  });
                  if (status) {
  
                  io.emit('news', news);
  
              }
              if (timerIndexWatch) {
                  clearTimeout(timerIndexWatch);
              }
  
              timerIndexWatch = setTimeout(RunScrapingIndexWatch, config.DelayExtractIndexWatch);                
          }
      });
  }
  </code></pre>
  
  <p>The problem is that in the web display (generated by the above code "news.indexwatch.push") for a brief instant the value of IWTime is appearing as undefined - I guess that is happening when the mysql connection query is running.  The mysql database is getting updated correctly.</p>
  
  <p>Is there a way to run the mysql insert query after the io.emit news action?  Any suggestions?</p>