I'm just getting started with Nodejs, so please bear with me
I store my DB setting on the first JS, connect.js
:
var mysql = require('mysql');
module.exports = function(connectDB) {
var connectDB = {};
connectDB.connection = mysql.createConnection({
//db params
});
connectDB.connection.connect(function(err) {
if (err) {
console.error('error connecting: ' + err.stack);
return;
}
console.log('connected as id ' + connection.threadId);
});
return connectDB;
};
Then I stored my query in another JS file, lets call it dbManager.js
:
var db = require('./connect')(connectDB);
var test_connection = connectDB.connection.query('SELECT * FROM `test`', function (error, results, fields) {
console.log(results);
});
exports.test = test_connection;
My goal is to pass the connection
variable from connect.js
to dbManager.js
, so I could use it for running some queries.
The above code return an error, which said the variable is not passed successfully to dbManager.js
:
ReferenceError: connectDB is not defined
Thanks in advance