节点mysql连接池导出

时间:2018-05-13 05:40:54

标签: mysql node.js electron lokijs

我正在使用electronjs创建一个应用程序。我创建了一个连接池,我将在我的项目中全局使用。用户可以选择需要连接的mysql服务器,并将选定的服务器配置保存在lokijs数据库中。当我们创建连接池时,我们将从lokijs数据库获取mysql连接详细信息。

现在我收到这样的错误。

  

未捕获的TypeError:无法读取null的“find”属性

请参阅以下代码

   var mysql = require('mysql');
   var loki = require("lokijs");

var db = new loki('config/config.json', {
    autoload: true,
    autoloadCallback: databaseInitialize,
    autosave: true,
    autosaveInterval: 1000 // save every four seconds for our example
});

function databaseInitialize() {
    // on the first load of (non-existent database), we will have no collections so we can 
    //   detect the absence of our collections and add (and configure) them now.
    var CurConnection = db.getCollection("CurConnection");
    if (CurConnection === null) {
        CurConnection = db.addCollection("CurConnection");
    }
}

var CurConnection = db.getCollection("CurConnection");
var rows = CurConnection.find({
    '$loki': {
        '$eq': 1
    }
});

var row = rows[0];

var servername = row.selservername;
var port = row.selport;
var dbname = row.seldbname;
var username = row.selusername;
var password = row.selpassword;

var connection = mysql.createPool({
    multipleStatements: true,
    host: servername,
    port: port,
    user: username,
    password: password,
    database: dbname
});

module.exports = connection;

1 个答案:

答案 0 :(得分:0)

在此代码后添加2-3秒的睡眠时间

var CurConnection = db.getCollection("CurConnection");
if (CurConnection === null) {
    CurConnection = db.addCollection("CurConnection");
}

或在此代码之前

 var rows = CurConnection.find({
    '$loki': {
        '$eq': 1
    }
  });