Abdullah Surati
Abdullah Surati

Reputation: 13

Storing data from a nested firebase object

I am working with an attendance web app which displays information month wise. Here is my structure of firebase database:Firebase Database Structure

To iterate over the data, i use this:

for(i = 0; i<months.length;i++){

    console.log(months[i]);

    var datesRef = firebase.database().ref('students/' + currentStudentSap + '/attendance/' + currentSem + '/' + months[i]);
    datesRef.on('value',function(snapshot){

        var dates = snapshot.val();
        var date = Object.keys(dates);

        for(j=0;j<date.length;j++){

            console.log(date[j]);

            var lectureRef = firebase.database().ref('students/' + currentStudentSap + '/attendance/' + currentSem + '/' + months[i] + "/" + date[j]);
            lectureRef.on('value',function(snapshot){

                var lectures = snapshot.val();
                console.log(lectures, typeof(lectures));

            }); 
        }
    });
}

Here is what I get on the console: Console Data

What I want to do now is save these stats in some data structure so that I can use them to produce graphical charts. For eg: "COSTtheory" is a subject and I want to calculate total conducted lectures and total present lectures for each month, and display month wise statistics (such as percentage attended) in HTML.

Upvotes: 0

Views: 75

Answers (1)

Simon Cadieux
Simon Cadieux

Reputation: 347

I don't know exactly how you insert data in Firebase but i would not iterate thru it like you do. Instead i would increment the monthly totals i need to make report each time data is inserted, that way i would be able to show live stats (kind of purpose of a live DB). I would use Firebase database transactions with cloud functions to achieve this. Transactions ensure data consistency.

Upvotes: 0

Related Questions