title: Variable not retaining value outside of function
body: <p>I am attempting to pass the data variable from the function to the app.get call.
  But the global variable 'data' in my function is not retaining the value returned from the database query. I believe this to be a scoping issue. I set the value for 'data' to null at the beginning to make the variable accessible throughout the function. I am not hitting either of the error conditions.</p>
  
  <pre><code>function getCommands(query) {
      var data = null;
      try {
  
      pg.connect(cString, function(err, client, done) {
          // Catch any connection errors, and display them to the screen
          if(err) {
              return console.error('could not connect to postgres', err);
          }
  
          client.query(query, function(q_err, result) {
  
          // Release the client back to the pool
          done();
  
          if(q_err) {
              return console.error('error running query', q_err);
          }
  
          // Data object with an empty array to hold the row information
          data = {"data":[]};
  
          for (var row in result.rows)
          {
              //console.log(result.rows[row]);
              data.data.push(result.rows[row]);
          }
          //Here, data, has the correct return values.
          console.log(data);
          });
  
      });
  
      }
      catch( e )
      {
          console.log(e);
      }
      //Here, data, is null.
      console.log(data);
      return data;
  
  }
  
  app.get('/clients/', function(req, res) {
      res.send(getCommands('SELECT clientid, clientname FROM hourglass.clients ORDER BY clientid ASC'));
  });
  </code></pre>
  
  <p>Could someone help me determine why 'data' is not retaining value outside of the pg.connect function?</p>