title: exported variables got undefined while functions get exported in node js
body: <p>I am creating the nodejs application which uses the mongodb.</p>
  
  <p>I am connecting to mongodb only once. I want to use <code>db</code> in all other api's so as to achieve the connection pooling.</p>
  
  <p>I have following code for mongodb connectivity:</p>
  
  <pre><code>var mongodb = require('mongodb');
  
  var MongoClient = require('mongodb').MongoClient;
  var db;
  
  var mongoUrl = "mongodb://localhost:27017/testDB";
  /**
   * Connects to the MongoDB Database with the provided URL
   */
  
  
  exports.connect = function(callback){
      if(!db){
          MongoClient.connect(mongoUrl, function(err, _db){
              if (err) { throw new Error('Could not connect: '+err); }
              db = _db;
              console.log(db);
              connected = true;
              console.log(connected +" is connected?");
              callback(db);
          });     
      }else{
          console.log("Not connected tis time as I am already connected");
          callback(db);
      }
  
  };
  
  
  exports.db = db;
  </code></pre>
  
  <p>I am calling <code>connect</code> only once when server starts from <code>app.js</code>. Whenever other api such as <code>signin, register</code> get called, they should simply use db that is exported.</p>
  
  <p>So my api calls will something like(please ignore the syntax error in api call :D):</p>
  
  <pre><code>var mongo  = require('./mongo');
  
  collection = mongo.db.collection("testCollection");
  // Here mongo.db id undefined
  collection.findOne({"name":"John"}, function(err, result){
     // Do your stuff with result here
  });
  </code></pre>
  
  <p>From other stackoverflow posts, I tried something like in mongo.js as</p>
  
  <pre><code>module.export{
  db: db,
  connect : function(callback){
    //function code goes here
  }
  }
  </code></pre>
  
  <p>But still I am getting the <code>undefined</code> for <code>mongo.db</code>
  How would I access mongo.db in my other files?
  Thanks</p>