When testing my code the console logs will be 2: 0
(multiple times), 3: 0
(once), 1: _
(multiple times where _
increases each time).
I am unsure as to how to get 1
to occur before 2
and 3
so that when I write to the database (after log 3) the value isn't 0
.
I am unsure how to approach this problem as I am inexperienced with firebase.
var count=0;
var total=0;
var ass_ref = admin.database().ref("modules/"+mod+"/Assignments");
return ass_ref.once("value", function(snapshot) {
snapshot.forEach(function(child) {
var name = child.key;
var ref = admin.database().ref("modules/"+mod+"/Assignments/"+name+"/"+uid);
ref.once("value", function(mark) {
count++;
total=total+ parseInt(mark.val(),10);
console.log("1:"+total); //Value increases each time but occurs after 3
});
console.log("2:"+total); //Value = 0 (occurs multiple times, in foreach)
});
console.log("3:"+total); //Value = 0, logs once, after "2"
I desire 1
to occur before 2
(I believe it would log as 1,2,1,2...) so that the final value logged at 3
matches the last value logged at 1
.