Coming from my recent question in this site (before I resort to the "Promise" technology), I came up an idea of storing the variable in the firebase database instead of making it a global var in the javascript so that I can use it in any function (even though in an asynchronous one). So I made this code:
function logIn(){
 var loggedUname = $("#loginUname").val();
 var loggedPword = $("#loginPword").val();
 
 var tailorRef = firebase.database().ref('tailors');
 var keyRef = firebase.database().ref();
 
 tailorRef.on('value', function(data){
  data.forEach(function(childData){
   if ( (loggedUname == childData.val().tUsername) && (loggedPword == childData.val().tPassword) ){
    currentKey = childData.key;
   }
  });
  keyRef.update({currentTailor: currentKey});
  alert("key = " + keyRef.currentTailor);
 });
 
}the .update method was successful since it stored the corresponding key for the logged in user:
but when I try to retrieve it using alert, it says UNDEFINED! I don't understand because I'm assuming that I'm calling from the root reference of the database, that's as low as I go.
I tried making a seperate .on() function for the keyRef, but it's still the same result.
keyRef.on('value', function(data){
  alert(data.currentTailor);
});Did I called the data in a wrong way?
 
     
    