I have two files named login.js
and dbMySql.js
.
login.js
is the main file that will run dbMySql.js
, which is a submodule for the DB connection.
I can run getQuery()
from dbMySql.js
by using object.method call
.
getQuery()
also works fine and retrieves data from the database.
How can I get this data to my Main module from the database module? How can I return res
variable to main function in login.js
module
Code For login.js:
var MySqlConnect = require('./dbMySql');
var app = express();
var dbConnect = MySqlConnect("localhost", "root", "root", "mydb");
dbConnect.getQuery("select * from `test`");
console.log(dbConnect.resD);
Code For dbMySql.js:
function MySqlConnect(dbhost, dbuser, dbpassword, dbname) {
var MySqlConnect = require('mysql');
var con;
this.dbhost = dbhost;
this.dbuser = dbuser;
this.dbpassword = dbpassword;
this.dbname = dbname;
this.resultData = 'Def';
con = MySqlConnect.createConnection({
host: dbhost,
user: dbuser,
password: dbpassword,
database: dbname
})
con.connect(function (err) {
if (err) throw err;
})
function getQuery(query1) {
con.query(query1, function (err, res) {
if (err) throw err;
})
return {
resD: res
};
}
return {
getQuery: getQuery,
};
};
module.exports = MySqlConnect;
I want Retrieved Data in Main module via return method or any that will run successfully.