Mert Fırat
Mert Fırat

Reputation: 15

Global variable not working in NodeJS undefined

Is it possible to define a global variable in a node.js function?

I want use the ko variable (declared in the getNumbers function) in other functions

function getNumbers(callback) {
    result = cio.query("SELECT numbers FROM rooms WHERE durum='1'", function (err, result) {
        if (err) throw err;
        callback((result.length > 0) ? result[0].numbers : "");
    });
}

var ko = getNumbers(function (result) {

console.log(result)
});


 var zo = ko;

"undefined"

46,16,55,12,74,48,77,42,4,29,14,81,13,35,39,85,7,54,27,66,41,9,17,82,31,21,57,79,62,56,11,49,3,33,64,8,83,88,61,18,58,63,51,90,22,71,67,69,75,6,2,34,30,25,38,28,68,5,50,15,87,19,65,36,45,24
C:\wamp64\www\node\st\rooms.js:497
        var datadad = zo[Math.floor(Math.random()*zo.length)];
                                                     ^

TypeError: Cannot read property 'length' of undefined
    at sayiver (C:\wamp64\www\node\st\rooms.js:497:54)
    at Timeout._onTimeout (C:\wamp64\www\node\st\rooms.js:513:5)
    at ontimeout (timers.js:436:11)
    at tryOnTimeout (timers.js:300:5)
    at listOnTimeout (timers.js:263:5)
    at Timer.processTimers (timers.js:223:10)

Upvotes: 0

Views: 3085

Answers (3)

Maayao
Maayao

Reputation: 91

Here is a quick fix to your code. as being said before, since you do not return this value from the function you have to handle this logic inside the callback. Also, even if you would return a value, ko would still be uninitialized (when you tried to assign it's value to zo), since you use async action to get something from DB, and you do not wait for the data before you assign.

function getNumbers(callback) {
    result = cio.query("SELECT numbers FROM rooms WHERE durum='1'", function (err, result) {
        if (err) throw err;
        callback((result.length > 0) ? result[0].numbers : "");
    });
}

var ko;
var zo;    

getNumbers(function (result) {
  ko = result;
  console.log(result);
  zo = ko;
});

here is an example for a code where zo will still be (given the function is not sync of course) undefined:

function getNumbers(callback) {
    result = cio.query("SELECT numbers FROM rooms WHERE durum='1'", function (err, result) {
        if (err) throw err;
        callback((result.length > 0) ? result[0].numbers : "");
    });
}

var ko;

getNumbers(function (result) {
  ko = result;
  console.log(result);
});
var zo = ko; //Not good!

Upvotes: 2

Dave jaydeep
Dave jaydeep

Reputation: 11

You can use this approach:

global.ko; // Declaration of the global variable - undefined
global.ko = 5; // Global variable initialized to value 5.

You can access like this:

var myNumber = global.myNumber;

Upvotes: 0

NikxDa
NikxDa

Reputation: 4187

You can, and your approach would be correct.

The reason ko (and zo, respectively) are undefined is because you are not returning anything from getNumbers, instead, you are using a callback.

Either handle the logic involving the variable inside your callback, or use async and await.

See: Asynchronous JavaScript

Upvotes: 2

Related Questions