title: Exporting Mysql Connection in nodeJS
body: <p>In my database.js I have</p>
  
  <pre><code>var Mysql = require('Mysql');
  var Jwt = require('jsonwebtoken');
  var bcrypt = require('bcrypt');
  
  var supersecretkey = 'JMDub_Super_Secret_key';
  
  var config = require('./config');
  var signupErrors = require('./Signuperrors.js');
  var sucessMsg = require('./SucessMessages.js');
  var App_errors = require('./error.js');
  var query = require('./queryDB.js');
  
  var connection = Mysql.createConnection({
                      "host": "******",  
                      "user": "****", 
                  "password": "***", 
                  "database": "***"
                  });
  
  connection.connect(function(err) {
    if (err) {
      console.error('error connecting: ' + err.stack);
      return;
    }
    console.log('connected as id ' + connection.threadId);
  });
  
  
  //Sign Up Methods 
  var createUser = function createwithCredentails(post,callback) {
      bcrypt.hash(post.password, 10, function(err, hash){
  //console.log('Cache Hash : +',hash);
      var createUserQuery = connection.query('INSERT INTO users SET ?',{"email":post.email,"password":hash,"username":post.username},function(err,result){
          if (err) {
              if (err.code == 'ER_DUP_ENTRY') {
                  //console.log(err.code);
                  callback(signupErrors.error_5000);
              }
              else callback(App_errors.error_1003);
          }
          if (result) {
              callback(sucessMsg.success_signup);
          }
      });
      });
  }
  
  //connection.query('SELECT * FROM Users Where Username = '' AND Password = ''');
  
  var validateUser = function ValidateUserWithUserNameAndPassword(post,callback) {
      var UserCheckQuery = connection.query('SELECT * FROM users WHERE email="'+post.email+'"',function(err, results, fields) {
      if (err){
          console.log(err);
          callback(App_errors.error_1000);
      }
      if (results.length == 1) {
      //console.log(results[0].password,post.password);
          var givenPassword = post.password;
          var DBhash = results[0].password;
              bcrypt.compare(givenPassword, DBhash,function(err, res) {
                  if (res) {
                      console.log('Password matched');
                       var token = Jwt.sign({"email":post.email,"username":post.username},supersecretkey, {
                          expiresIn: 60*60*5 // expires in 5 hours
                       });
  
                       callback({
                          message:{
                              "success":1,
                              "description":"sucessfully logged in - please cache the token for any queries in future",
                              "environment":"test",
                              "errorCode":null
                    },
                    "token":token
                });
  
                  }
                  if (!res) {
                      console.log('password doesnt match');
                      callback(signupErrors.error_6000);
                  }
                  if (err) {
                      console.log('Error Comparing Passwords');
                      callback(App_errors.error_1004);
                  }
  
              });
  
      }
      else{
          callback(signupErrors.error_6000);
      }
      });
  };
  
  var isauthenticate = function isauthenticated(post,route,callback) {
      if (post.headers.token) {
          Jwt.verify(post.headers.token, supersecretkey, function(err, decoded) {
          if (decoded) {
              //console.log(decoded);
          //From this part the user is Sucessully Authenticated and autherization params can be extracted from token if required
          //Write Business Logic in future as per the requirement
  
          //Operation 1 - Update Profile
          //Profile Details consists of {1.first name 2.last name 3. profile pictur(base 64 encoded) 4.further settings in future that can be added to DB if required}
          if (route == '/update-profile') {
            query.updateProfile(connection,decoded.email,post.body,function(response) {
              callback(response);
            });
          }
  
              //callback({"message":"is a valid token"});
          }
          if (decoded == null) {
              console.log('is not a valid token');
              //callback(App_errors.error_1000);
          }
          if (err) {
              console.log('error verifying token');
              callback(App_errors.error_1000);
          }
  });
  
      }
      else{
          callback(App_errors.error_1001);
      }
  };
  
  module.exports = {
      validateUser:validateUser,
      createUser:createUser,
      isauthenticate:isauthenticate,
      connection:connection
  }
  </code></pre>
  
  <p>I am exporting connection object to queryDB.js file. But when I try to log the exported connection object I get undefined object. Why is this happening?</p>
  
  <p>When I pass connection object as function argument, everything works fine. Not sure why?</p>
  
  <p>below is queryDB.js file</p>
  
  <pre><code>    var errors = require('./error.js')
  var Dbconnection = require('./Database.js').connection;
  var updateProfile = function profiledata(connection,email,data,callback) {
      console.log(Dbconnection);
      if ((!data)|| (Object.keys(data).length &lt; 1)) {
          //console.log(data);
          callback(errors.error_1001);
      }
  
      else{
          callback({"message":"update Sucesss"});
          //console.log(connection);
          //var updateData = mapProfileDataTomodel(data);
          //console.log(updateData);
       connection.query('SELECT * FROM users WHERE email = "'+email+'"',function(err, result,feilds) {
         if (err) throw err;
         if (result) {
         console.log(result);
         }
       });
      }
  }
  
  var mapProfileDataTomodel = function mapProfileDataTomodel(data) {
      var profileDataModel = {};
  for (var key in data) {
      //console.log('looping and mapping data');
          if (data.firstname) {
              profileDataModel.firstname = data.firstname;
          }
          if (data.lastname) {
              profileDataModel.lastname = data.lastname;
          }
          if (data.profilepic) {
              profileDataModel.profilepic = data.profilepic;
          }
      }   
  return profileDataModel;
  }
  
  
  module.exports = {
      updateProfile:updateProfile
  }
  </code></pre>
  
  <p>I have commented out connection object log via function arguments.</p>
  
  <p>So, Why I am unable to get the connection object that is exported? But I used the same exported connection object in my app.js file. It did work fine there. </p>