mad-hin
mad-hin

Reputation: 141

javascript cannot call global variable

I am having a situation that I created a global variable in JavaScript and I assigned it in an addEventListener. However, when I want to call the variable in another if case, it says it is undefined
Here is my code in JavaScript

let userID; // the global variable (btw I have tried both let and var)

// Check if this page is the login in page
if (fileName[0] === "index.html") {
    var signIn = document.getElementById("signInbnt");

    // Sign in 
    signIn.addEventListener('click', function () {
        let email = document.getElementById("email").value;
        let pwd = document.getElementById("pwd").value;
        userID = email // Assign the variable
        console.log(userID)// It work as my expectation (i.e. "[email protected]")

        signInFunction(email, pwd);
    });
    // End of Sign in

} else if (fileName[0] === "system.html") {
    console.log(userID) // It shows "undefined"
    firebase.auth().onAuthStateChanged(function (user) {
        if (user) {
            let uid = user.uid;
            getUserName(uid);
        }
    });
}

For your reference here is the code of the signInFunction

function signInFunction(email, pwd) {
    firebase.auth().signInWithEmailAndPassword(email, pwd).then(function () {
        firebase.auth().onAuthStateChanged(function (user) {
            if (user) {
                db.ref('users/' + user.uid + '/name').once('value').then(
                    name => {
                        passwordUpdate(name.val(), pwd)
                    }
                )
            }
        });
        document.location.href = 'system.html';
    }).catch(function (error) {
        if (error != null) {
            // Get Error Message
            let errorMessage = error.message;

            // Make The Horizontal Rule Be Visible
            document.getElementById("login-hr").style.visibility = "visible";

            // Load Error Message
            document.getElementById("login-error").innerHTML = errorMessage;

            // Make The Error Message Be Visible
            document.getElementById("login-error").style.visibility = "visible";

        }
    })
}

Could any tell me why I cannot call the global variable and how I can solve it. Thank you very much

Upvotes: 1

Views: 83

Answers (2)

Liu Lei
Liu Lei

Reputation: 1297


let userID; // the global variable (btw I have tried both let and var)

// now userID === 'undefined
// Check if this page is the login in page
if (fileName[0] === "index.html") {
    ...

    // Sign in 
    signIn.addEventListener('click', function () {
        ...
        userID = email // Assign the variable
        // userId ===  email
        ...
    });
    // End of Sign in

} else if (fileName[0] === "system.html") {
    console.log(userID) // It shows "undefined"
    // u didn't assign value to userID, so it still undefined
    
}

Upvotes: 1

BillJustin
BillJustin

Reputation: 274

it returns undefined because when you first declared userID it doesnt have any value yet. it will only have a value when your code runs the first if-statement, which is the email. however, if your code skips the first if-statement because it hasnt met the set condition, it will run the else-if statement which do not assign anything to your userID variable. thats why when you log it into the console it returns undefined

Upvotes: 2

Related Questions