This was working for me before, but without having changed anything, it seems to have broke. I'm declaring a variable outside of a code block, and then reassigning the value in a loop. The value prints correctly within the loop, but I get an empty string when I print outside the block.
var cnaKey = ""; // Instantiating this variable outside the code block below
var cnaRef = rootRef.child("Activities/"+selectedPatient+"/"+selectedDate);
cnaRef.once('value', function(cnaSnap){
cnaSnap.forEach(function(cnaChild) {
cnaKey = cnaChild.key;
console.log(cnaKey); // This is printing the correct value
});
});
console.log(cnaKey) // Printing an empty string