node.js - made module, but can not reach to functon -


firstly, english skill poor i`m sorry that

i made code hide private functions & vars other

db_obj.js

module.exports = (function() { var _mysql = require('mysql'); var _self = null; var _connflag = false;  var _mysqlconfig = {     host: "127.0.0.1",     port: 3306,     user: "tester",     password: "*****",     database: "*****" };  var _conn = null;  var _init = function(){     _conn = _mysql.createconnection(_mysqlconfig);      _conn.connect(function(err) {                       if (err) {                 console.log(err);         }         _connflag = true;     });  };    var db_obj = function (args) {     _self = this;      if (args) {         _mysqlconfig.host = (args["host"]) ? args["host"] : "127.0.0.1";         _mysqlconfig.port = (args["port"]) ? args["port"] : "3306";         _mysqlconfig.user = (args["user"]) ? args["user"] : "*****";         _mysqlconfig.password = (args["password"]) ? args["password"] : "*****";         _mysqlconfig.database = (args["database"]) ? args["database"] : "*****";     }      this.init = function () {         _init();     };  };  return db_obj;  })(); 

main.js

var db_obj = new require('./db_obj'); db_obj.init(); 

but code error occur it:

typeerror: db_obj.init not function

please. let me know how can fix it?

thanks.

change db_obj below

var db_obj = function (args) {     _self = this;      if (args) {         _mysqlconfig.host = (args["host"]) ? args["host"] : "127.0.0.1";         _mysqlconfig.port = (args["port"]) ? args["port"] : "3306";         _mysqlconfig.user = (args["user"]) ? args["user"] : "*****";         _mysqlconfig.password = (args["password"]) ? args["password"] : "*****";         _mysqlconfig.database = (args["database"]) ? args["database"] : "e3multicore_v1";     }      _self.init = function () {         _init();     };      return  _self; }; 

in main.js

var db_obj = new require('./db_obj');  db_obj().init(); 

Comments

Popular posts from this blog

sql - VB.NET Operand type clash: date is incompatible with int error -

SVG stroke-linecap doesn't work for circles in Firefox? -

python - TypeError: Scalar value for argument 'color' is not numeric in openCV -