title: Node.js mysql results to array
body: <p>I've been slowly learning node.js so that I can integrate things better between a lot of our current ops (mysql) and Xero accounting.</p>
  
  <p>I have several tables in mysql, one each for ["invoices","accounts","items","organisations","receipts","taxRates","users","trackingCategories"]
   and each of those tables has a JSON column with the same name as the table it's in.  This is NOT a json question.</p>
  
  <p>Sending a query like ""select "+ wewant1[i] + " from "+wewant1[i]" is basically simple "select invoices from invoices" and easy to iterate through. </p>
  
  <p>I can get the mysql results to list, but need to get each separate "list" as an array of results.</p>
  
  <p>I would ultimately like to be able to reference the results from "select invoices from invoices" as an "invoices" array in node (invoices[0], invoices[1], etc).</p>
  
  <p>I've tried avoiding "callback hell" and still cannot get this to work... 
  Any tips are very welcome!</p>
  
  <p>Here is the current code:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>var mysql = require('mysql');
  
  var con = mysql.createConnection({
    host: "10.0.1.103",
    user: "badayaba",
    password: "yadabuba",
    database: "xeroetc"
  });
  
  (async function() {
  
    let wewant1 = ["invoices", "accounts", "items", "organisations", "receipts", "taxRates", "users", "trackingCategories"];
  
    function getmydata(sql, result, callback) {
      var query = con.query(sql);
      query.on('result', function(row) {
        callback(null, row);
      });
    };
  
    for (let i = 0; i &lt; wewant1.length; i++) {
      var sql = "select " + wewant1[i] + " from " + wewant1[i];
      getmydata(sql, wewant1[i], function querydata(err, result) {
        console.log(err || result);
        return result;
      });
    };
    con.end();
  })();</code></pre>
  </div>
  </div>
  </p>
  
  <hr>
  
  <p>20180910 22:00 GMT-6
  Thanks Steven!
  I think I got it with this:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>const XeroClient = require('xero-node').AccountingAPIClient;
  const XeroErrors = require('xero-node').XeroError;
  var mysql = require('mysql');
  
  const config = {
      appType: "private",
      consumerKey: "_____",
      consumerSecret: "_____",
      privateKeyPath: "../../../ssl/_____.pem"
    };
  
  var con = mysql.createConnection({
      host: "_____",
      user: "_____",
      password: "_____",
      database: "xeroetc"
  });
  
  
  (async function(){
  
  let wewant1 = ["invoices","accounts","items","organisations","receipts","taxRates","users","trackingCategories"];
  
  let xero = new XeroClient(config);
  
  function getmydata(it, callback) {
         var sql = "select "+it+" from "+it;
         con.query(sql, function (err, result, fields) {
          if (err) throw err;
          callback(null,result);
      });
    };
    
    const promises = wewant1.map(it =&gt; {
       return new Promise((resolve, reject) =&gt; {
         getmydata(it, function querydata(err, result) {
           if (err) {
             reject(err);
           } else {
             resolve(result);
           }
         });
       })
    });
  
    Promise.all(promises)
    .then(results =&gt; {
      //results is a array of the resolved promises
      invoices=results[0];
      accounts=results[1];
      items=results[2];
      organisations=results[3];
      receipts=results[4];
      taxRates=results[5];
      users=results[6];
      trackingCategories=results[7];
      console.log(invoices);
      })
    .catch(err =&gt; {})
      .then(() =&gt; {
          con.end();
      })
  })();</code></pre>
  </div>
  </div>
  </p>