nodejs TypeError:this.connect不是函数

时间:2017-05-15 19:11:53

标签: javascript node.js function

当我启动nodejs应用程序时,它无法找到一个非常重要的函数:TypeError: this.connect is not a function at C:\Users\Jonas\.AtomProjects\PoliticsBrowserGame\app\database.js:89:12 at tryToString (fs.js:456:3) at FSReqWrap.readFileAfterClose [as oncomplete] (fs.js:443:12)

这是我想要只存在一次的数据库类,因为我只想要一个连接用于应用程序而不是多个连接。

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;

修正了,感谢Ajay和Lennart Hase。

但我也有另一个问题,在我的代码中我设置了module.exports = Database; 当我想在另一个文件中使用数据库时,它是未定义的。我想在另一个文件中使用它,我只想使用它的实例,因为我只想为应用程序运行一个连接。

2 个答案:

答案 0 :(得分:0)

尝试更改this.connectreadFile回调this不代表您的数据库实例。因此,我们需要将this引用存储在某些变量中,例如var self = this

//filename is database.js
function Database() {
   ....
   ....
   var self = this;
   fs.readFile(process.cwd() + "/config.json", 'utf8', function(err, data) {
     if(err) throw err;
     config = JSON.parse(data);
     self.connect();
   });
   ....
}
module.exports = Database;

更新:从外部访问此数据库模块。

var dbModule = require("./database.js");
var database = new dbModule();
// Give some time to read config.json and connect
database.getMainConnection();

答案 1 :(得分:0)

我认为您的问题是您在其他方法中调用了this,请尝试使用以下代码替换代码的最后一部分;

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

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

thisfs.readFile()方法的回调方法中有一个新定义,因此你试图在回调方法中调用一个显然不存在的连接方法。您实际上是在尝试调用父方法,这将是实现此目的的一种方法。