Search code examples
node.jsnode-mysql

Update variable value in module


I'm a real newbie in node.js so pls understand my possible stupidity

I'm trying to use a external file to serve as a module so I can use it in other files. The project is bigger than this but let's say my module is:

var Tools = module.exports = {
  result_arr: [],
  object_data: {
    times  : [],
    temps1 : [],
    temps2 : [],
    temps3 : [],
    temps4 : [],
    levels : [],
    flows  : []
  },
  getLastNRows: function (whereIsData, DB_info, table, NRows) {
    if (whereIsData == "MySQL") {
      function setValue (value) {
        Tools.result_arr = value;
      }
      function dataArray2Object (array_data) {
        Tools.object_data.times  = array_data.map(row => row.timestamp);
        Tools.object_data.temps1 = array_data.map(row => row.temp1);
        Tools.object_data.temps2 = array_data.map(row => row.temp2);
        Tools.object_data.temps3 = array_data.map(row => row.temp3);
        Tools.object_data.temps4 = array_data.map(row => row.temp4);
        Tools.object_data.levels = array_data.map(row => row.level_ice_bank);
        Tools.object_data.flows  = array_data.map(row => row.flow);
      }
      var queryString = "SELECT timestamp, temp1, temp2, temp3, temp4, level_ice_bank, flow FROM " +
                        table + " ORDER BY id DESC LIMIT " + NRows + ";";
      var connnection = mysql.createConnection(DB_info);
      connnection.connect(function(err) {
        console.log("connected");
        if (err) throw err;
      });
      connnection.query(queryString, function (err, rows) {
        console.log("queried");
        if (err) throw err;
        setValue(rows);
        dataArray2Object(Tools.result_arr);
        console.log(Tools.result_arr);
        console.log(Tools.object_data);
      });
    }
    else {
      console.log("Function only accepts data stored in MySQL.\n(u still have to improve...)");
      return;
    }
};

The variable object_data is supposed to be used in a main file. This way, whenever I call getLastNRows, I expect object_data to be updated by the operations in getLastNRows. The main file would be:

var tools = require('./tools');

var where2save = "MySQL";
var info_db = {
  host    : "127.0.0.1",
  user    : "root",
  password: "xxxx",
  database: "mydb",
  port    : 3306
};
var table = "tempdata";
var NRows = 4;

tools.getLastNRows(where2save, info_db, table, NRows);

console.log(tools.object_data);

What is observed is that, in fact, tools.object_data is not updated by getLastNRows in the main file, although console.log(Tools.object_data); from the tools.js (module) file logs the updated values. So my question is:

How can I make getLastNRows update tools.object_data (which is empty when created) in the main file?


Solution

  • Is getLastNRows asynchronous? Cuz it seems to me that is the cause of the problem.

    It calls the getLastNRows in main which then calls connection.query, which gets put on a worker thread then immediately continues to the console.log where tools.object_data has not been updated.

    Try:

    getLastNRows: function (whereIsData, DB_info, table, NRows, cb) {
        // ...
        connnection.query(queryString, function (err, rows) {
                console.log("queried");
                if (err) throw err;
                setValue(rows);
                dataArray2Object(Tools.result_arr);
                console.log(Tools.result_arr);
                console.log(Tools.object_data);
                cb()
        });
        // ...
    }
    
    // in main
    tools.getLastNRows(where2save, info_db, table, NRows, function() {
        console.log(tools.object_data);
    });