Ethan
Ethan

Reputation: 2087

Java/Firebase Script Executing Multiple Times

I am having an interesting issue. The general idea of what I am doing is pulling data from a Firebase database, and populating a table based on that data. Everything runs perfectly during initial population--cells and rows are populated as they should be, but the weird issue is that the scripts seem to execute again randomly. I've logged the incoming data to the console, and can see it print twice after some amount of time.

This second execution does not happen if I am to navigate between pages, or reload the page--in either of those cases everything works as it should. The problem SEEMS to happen when I log back into my computer after locking it??? Does anybody have ANY idea what could be going on here? Relevant portion of script below:

const table = document.getElementById('myTable');
firebase.auth().onAuthStateChanged(firebaseUser => {
    if (firebaseUser) {
        let user = firebase.auth().currentUser;
        let uid = user.uid;
        const dbRef = firebase.database().ref().child("data/" + uid);
        dbRef.once('value', snap => {
            var dataCount = snap.child("secondData").numChildren();
            var datalist = snap.child("secondData").val();
            var dataArray = Object.keys(datalist).map(function(k) {
                return datalist[k]
            });
            pullAllInfo(dataCount, dataArray);
        });
    }
});

function pullAllInfo(count, array) {
    let k = 0;
    let dataArray = [];
    for (i = 0; i < count; i++) {
        let specificRef = firebase.database().ref().child("secondData/" + array[i]);
        specificRef.once('value', snap => {
            var optionsTag = array[k];
            k++;
            var dataId = snap.child("id").val();
            var dataName = snap.child("name").val();
            var dataCount = snap.child("data").numChildren();
            dataArray.push(dataId, dataName, dataCount, optionsTag);
            if (k == count) {
                buildTable(dataArray);
                console.log(dataArray);
            }
        });
    }
}

As you can see from the code above I AM calling .once() for each reference, which would prevent data duplication from the typical .on() call. Just cant seem to figure this one out. ALSO I have an iMac, just for anyone curious about my potential computer unlock diagnosis.

Thanks all!

Upvotes: 0

Views: 104

Answers (2)

Ethan
Ethan

Reputation: 2087

I solved this issue by creating another global boolean called preLoaded. At the beginning, it is set to false and, once the data is loaded and passed off to build the table, it is set to true. It now looks like this:

if(k == count && preloaded == false){
            preloaded = true;
            console.log(dataArray);
            buildTable(dataArray);
        }

All set!

Upvotes: 0

imjared
imjared

Reputation: 20554

Most likely, the auth state is changing and setting off your function. Try throwing a log under firebase.auth().onAuthStateChanged like this:

firebase.auth().onAuthStateChanged(firebaseUser => {
    console.log( 'auth state changed', firebaseUser );
    if (firebaseUser) {

My guess is that you'll see that the AuthState is changing when you log out/log in from your computer.

Upvotes: 1

Related Questions