node mysql connection pool exports

341 Views Asked by At

I am making an app using electronjs. I have created one connection pool which i will use globally in my project. A user can select which mysql server he need to connect and the selected server configuration is saved in a lokijs database. When we create the connection pool we will get mysql connection details from the lokijs database.

Now I am getting an error like this.

Uncaught TypeError: Cannot read property 'find' of null

Please see the code below

   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

There are 1 best solutions below

0
On

Add sleep of 2-3 seconds after this code

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

or before this code

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