2017-05-15 4 views
-1

이것은 응용 프로그램에 하나의 연결 만 허용하고 다중 연결은 필요 없기 때문에 한 번만 존재하려는 내 데이터베이스 클래스입니다. 나는 그것의 정의되지 않은 다른 파일에서 데이터베이스를 사용하려는 경우 내 코드에서Node.js 모듈 클래스는 undefined를 반환합니다.

var mysql = require('mysql'); 
var fs = require("fs"); 
var eventEmitter = require("./events.js"); 

function Database() { 
    this.connection; 
    this.poolCluster; 

    var host; 
    var username; 
    var password; 
    var db; 

    var config; 
    var clusterConfig = { 
    removeNodeErrorCount: 5, 
    restoreNodeTimeout: 1000, 
    defaultSelector: 'ORDER' 
    }; 

    var poolConfig = { 
    acquireTimeout: 10000, 
    waitForConnections: false, 
    connectionLimit: 10, 
    queueLimit: 0 
    }; 

    this.connect = function() { 
    this.connection = mysql.createConnection({ 
     host: config.mysqlHost, 
     user: config.mysqlUsername, 
     password: config.mysqlPassword, 
     database: config.mysqlDb 
    }); 

    this.connection.connect(function(err) { 
     if(err) { 
     console.error("Connection couldn't established at " + config.mysqlHost + " (user: " + config.mysqlUsername + ")" 
     + "\nError: " + err); 
     return; 
     } 

     console.log("Connected to mysql server at " + config.mysqlHost + " (user: " + config.mysqlUsername + ")"); 

     this.poolCluster = mysql.createPoolCluster(clusterConfig); 

     this.poolCluster.add("APP", poolConfig); 
     this.poolCluster.add("ACCOUNTS", poolConfig); 
     this.poolCluster.add("GAME", poolConfig); 

     console.log("Created Connection Clusters\n- APP\n- ACCOUNTs \n- GAME"); 
     eventEmitter.emit("MysqlConnectionReady"); 
    }); 
    }; 

    this.getMainConnection = function() { 
    return this.connection; 
    }; 

    this.getAppConnection = function() { 
    this.poolCluster.getConnection("APP", 'ORDER', function(err, connection) { 
     if(err) throw err; 

     return connection; 
    }); 
    }; 

    this.getAccountsConnection = function() { 
    this.poolCluster.getConnection("ACCOUNTS", 'ORDER', function(err, connection) { 
     if(err) throw err; 

     return connection; 
    }); 
    }; 

    this.getGameConnection = function() { 
    this.poolCluster.getConnection("GAME", 'ORDER', function(err, connection) { 
     if(err) throw err; 

     return connection; 
    }); 
    }; 


    fs.readFile(process.cwd() + "/config.json", 'utf8', function(err, data) { 
     if(err) throw err; 

     config = JSON.parse(data); 
     this.connect(); 
    }); 
} 

module.exports = Database: 

나는 module.exports = Database; 설정합니다. 이 파일을 다른 파일에서 사용하고 싶습니다. 앱 인스턴스를 실행하는 데 하나의 연결 만 필요하기 때문에 인스턴스 만 사용하고 싶습니다.

하지만 require('./Database.js'j;을 사용하고 반환하는 VAR를 사용하는 경우 정의되지 않은

+1

그것은 복사/붙여 넣기 버그 않는 한, 문제는'module.exports = 데이터베이스가 여기에 있습니다 : 대신 module.exports = Database

는, 인스턴스를 생성하고이 같은 모듈로 있음을 수출하려고 콜론이 아닌 세미콜론이되어야합니다. –

+1

데이터베이스가 필요할 때 데이터베이스를 어떻게 호출합니까? 실제로'new'로 구성합니까? – aug

+0

Node.js는 모듈을 실제로 캐시하므로 모듈을 여러 번 저장해야 모듈 내용을 다시 평가하지 않습니다 (모듈 경로가 다른 경우 몇 가지 예외가 있음) –

답변

0

당신이 (당신의 코드에서와 같이)는 JS 함수로 클래스를 정의하는 의사 고전 OOP 접근 방식을 사용하려면, 당신은 인스턴스를 것 new 키워드가있는 개체입니다. 해야`:

const db = new Database(); 
module.exports = db 
관련 문제