title: Passing data from model to router in node js
body: <p>I am trying to pass some data from my db to the router which then passes the data to the view.</p>
  
  <p>My model code : </p>
  
  <pre><code>var mysql = require('mysql');
  
  var connection = mysql.createConnection({
    host:       'localhost',
    user:       'root',
    password:   '',
    database:   'test'
  });
  
  var result; // empty var which should later be filled with the querys result
  
  connection.connect();
  
  var query = connection.query('SELECT * FROM users', function(err, res, fields) {
    if (err) throw err;
  
    result = res; // overwrite result with the querys result
    console.log(res); // This prints out everything I need
  });
  
  
  module.exports = {
    data: result // should contain the query result (= 2 objects in this case)
  }
  </code></pre>
  
  <p>Now to my route file : </p>
  
  <pre><code>var express = require('express');
  var router = express.Router();
  
  var Users = require('../models/users');
  
  console.log(Users.data);
  /* GET home page. */
  router.get('/users', function(req, res) {
      res.render('api', { data: Users.data, title: "Test API Output" });
  });
  
  module.exports = router;
  </code></pre>
  
  <p>When I console.log Users or Users.data I get undefined. I don't really get why this is the case. How else am I supposed to pass data along the files.</p>
  
  <p>All help is gladly read :) Thank you.</p>