Search code examples
javascriptnode.jsnode-mysql

require function and variable from another file = nodejs


I am trying to offload some of the bulk from my app and came across this issue.

Here is my code:

// foo.js:

var mysql = require('mysql');

module.exports = {
  home: function () {
    return mysql.createConnection({
      host : '2.2.2.2',
      user: 'admin',
      password: 'xxxxxx',
      database : 'mlb'
    });
  }

// bar.js:

var dbConnect = require('./foo.js');

dbConnect.home.query('SELECT * from batters;', function(err, res, body) {
  if (err) throw err;
  var string_data = JSON.stringify(res);
  var jsonData = JSON.parse(string_data);
  console.log(jsonData);
});

The error i receive is undefined is not a function... What am I doing wrong?


Solution

  • Wouldn't home need to be like this:
    dbConnect.home().query