0

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.

Ondrej K.
  • 8,841
  • 11
  • 24
  • 39
  • Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – str Jan 20 '19 at 12:13

1 Answers1

0

Vary Simple Answer I have found by run and execute after efforts.

just pass res form db to callback function parameter and run this fucntion from main module