1

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

现在我收到这样的错误。

未捕获的类型错误:无法读取 null 的属性“查找”

请看下面的代码

   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;
4

1 回答 1

1

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

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

或在此代码之前

 var rows = CurConnection.find({
    '$loki': {
        '$eq': 1
    }
  });
于 2018-10-17T10:24:14.303 回答