Lolization
Lolization

Reputation: 69

How to define a variable that is undefined?

I have the following code:

if (!config.callers[uID].calls) {
    console.log(config.callers[uID].calls);
    config.callers[uID].calls = 0;
    saveConfig();
    console.log(config.callers[uID].calls);
}

Where I have a JSON file that is called config, and has a property callers which is a collection of user IDs, and each one has a property calls. saveConfig() is a function that saves the config file.

As you can see, I am checking if the calls property is undefined, and if true then it defines it at 0. I also tried defining it to different stuff but it didn't seem to work. It just stays undefined.

Upvotes: 1

Views: 75

Answers (1)

Saeed
Saeed

Reputation: 5488

Try

if(!config.callers.hasOwnProperty("uID")) {
    console.log(config.callers);
    config.callers[uID] = {};
    config.callers[uID].calls = 0;
    saveConfig();
    console.log(config.callers[uID].calls);
} else if(!config.callers[uID].hasOwnProperty("calls")) {
    console.log(config.callers[uID]);
    config.callers[uID].calls = 0;
    saveConfig();
    console.log(config.callers[uID].calls);
}

Upvotes: 3

Related Questions