0

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!

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Ok I will give this a try, right after my current potential solution either succeeds or fails (I will out of curiosity)
0

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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.