0
messageRef1.on("child_added", function(snap) {
  console.log(snap.val());
  mylat=snap.val();
})

document.write(mylat);

the console prints out the correct data and i could even use document.write within that function, but im unable to use the data anywhere else in the code. I understand this probably because firebase works asynchronously and basically the rest of the code doesnt wait for firebase. i saw this How can i access firebase variable outside firebase function but i dont understand how i could simply implement something here. thanks

Community
  • 1
  • 1
Yehuda Clinton
  • 375
  • 4
  • 12

1 Answers1

0

I think I found the solution to my own problem. any function that needs firebase could be called from within (snap). So in this case i could

    messageRef1.on("child_added", function(snap) {
      console.log(snap.val());
      mylat=snap.val();
      writeIt();//only calls after firebase loads
    })

 function writeIt(){
    document.write(mylat);
Yehuda Clinton
  • 375
  • 4
  • 12