0

I'm using a index.js file and a check.js file in Node.js. I use check.js to check every 500 ms if some value in my databases has changed. If so, the value of variable currentInput changes. I want to use this variable currentInput in index.js so, that I get the current value of it. I tried searching about in but the solutions I've found don't give me the current value back.

In check.js:

var currentInput;
.
.
.

var check = function(){
    pool.query('SELECT someValue FROM table WHERE id=1',function(err,rows){
        if(err) throw err;
        var newInput = rows[0].someValue;
        if(currentInput!=newInput){
            currentInput=newInput;
            }
        console.log('Current value:', currentInput);
    });
    setTimeout(check, 500);
}

In index.js I'd like to use it something like:

var x = function(currentInput);
  • Is check.js a module? How are you initializing the "check" function? If it's done as a module, then it should be relatively easy to scope the currentInput value so it's accessible to the parent (index.js). – egandalf Jan 17 '17 at 15:50

2 Answers2

0

You can export your function as a module. Then load it and call from index.js.

check.js

exports.check = function() {
    pool.query('SELECT someValue FROM table WHERE id=1',function(err,rows){
        if(err) throw err;
        var newInput = rows[0].someValue;
        if(currentInput!=newInput){
            currentInput=newInput;
        }
        return currentInput);
    });  
};

index.js

var check = require("./path/to/check.js");

setTimeout(function(){
    var x = check.check;
}, 500);
Gerard Cuadras
  • 1,655
  • 1
  • 17
  • 23
  • I've tried this, which partly works. I've removed the `if(currentInput!=newInput){ currentInput=newInput; }` and changed that into a `callback(newInput)` and put the `setTimeout` into index.js. However, on some values it works perfecly fine, on other values I get the error `throw err; // Rethow non-MySQL errors` followed by `TypeError: Invalid hex string`. What could be causing this? –  Jan 20 '17 at 14:59
  • @krishann_ run the queries directly into your SQL server to see what response you are getting. I don't know exactly what does your function to tell you why are you getting this error. – Gerard Cuadras Jan 21 '17 at 16:57
  • 1
    Got rid of the error. I was creating a `Buffer` in my index.js file, which contained the variable with the value I wanted to use from checker.js. I've put that Buffer in a try/catch block, now I can use the values which didn't work before. Seems the error had nothing further to do with my SQL server –  Jan 25 '17 at 15:38
0

You can use the GLOBAL variable. The GLOBAL variable is (yes you were right) global.

For example:

//set the variable
global.currentInput = newInput;
// OR
global['currentInput'] = newInput;

//get the value
var x = global.currentInput;
// OR
var x = global['currentInput'];

Note that this may not be the most efficient way of doing this, and people do not like this way at all (https://stackoverflow.com/a/32483803/4413576)

To use the global variable in different files, they have to be "connected with each other".

// index.js
require('./check.js')
Community
  • 1
  • 1
Gustav G
  • 459
  • 3
  • 10