title: node.js mysql result into a variable
body: <p>I've been using mountebank to do some stubbing for performance testing and its an awesome tool. The functional teams have asked if it can be repurposed to support functional testing and I'd said i'd have a look.</p>
  
  <p>What I want to achieve is to select from a mysql database an account number and its account balance and then return the balance to the client (in this case a jmeter harness)</p>
  
  <pre><code>function (request, state, logger) {
  
  logger.info('GBG - getAccountBalance');
  
  var mysql = require('mysql');
  var result = '';
  
  var con = mysql.createConnection({
  host: "localhost",
  user: "user",
  password: "password",
  database: "customer"
  });
  
  con.connect(function(err) {
      if (err) throw err;
      console.log("Connected!");
  });  
  
  con.query('select * from accounts', function (err, rows, fields) {
      if (err) throw err;
      console.log(rows);
      console.log('accountNumber is : ', rows[0].accountNumber);
      result = rows[0].accountNumber;
  });
  
  console.log('result is : ', result);
  var response = result;
  
  return {
      headers: {
                  'Content-Type': 'application/xml',
                  'Connection': 'Keep-Alive'
              },
      body: response
  };      
  
  }  
  </code></pre>
  
  <p>The result of the console log is:</p>
  
  <pre><code>result is :
  Connected!
  [ RowDataPacket { accountNumber: 777777, accountBalance: 777 } ]
  accountNumber is :  777777
  </code></pre>
  
  <p>Not sure what I'm doing wrong and why the result is : lines comes up first despite being later in the code.</p>
  
  <p>Any advice appreciated.</p>
  
  <p>Full disclosure, I've been using mountebank for about two weeks so I'm a real beginner.</p>