Yun Song
Yun Song

Reputation: 39

How to extract firebase snapshot value from one function to another function variable?

How to get the value that is snapshot from firebase and bring the value to other function . the school ID value that retriev from first function will the the variable inside the second function. How can I return the school ID value so that I can use the school ID inside checkEmpty?

 document.getElementById("newSchoolName").onchange=function (){
        var schoolName=document.getElementById("newSchoolName").value;
        var schoolID;
         firebase.database().ref('School').orderByChild("schoolName").equalTo(schoolName).once("value", function(snapshot) {
             
              snapshot.forEach(function(childSnapshot) {
                var  snapshoolID = childSnapshot.child("schoolID").val();
                 schoolID = snapshoolID;
                  console.log(schoolID);
                 
                  
              });
         
            });
       
        
        
    }

function checkEmpty(){
 
    [school ID value bring to here ]


}

Upvotes: 0

Views: 349

Answers (1)

Harry Tom
Harry Tom

Reputation: 424

make a function with parameter like this

document.getElementById("newSchoolName").onchange=function (){
    var schoolName=document.getElementById("newSchoolName").value;
    var schoolID;
     firebase.database().ref('School').orderByChild("schoolName").equalTo(schoolName).once("value", function(snapshot) {
         
          snapshot.forEach(function(childSnapshot) {
            var  snapshoolID = childSnapshot.child("schoolID").val();
            schoolID = snapshoolID;
            console.log(schoolID);

            checkEmpty(schoolID);
             

              
        });
     
    });
     
}

function checkEmpty(data){
    //the variable data has the value of 'school ID'
    console.log(data)
}

Upvotes: 1

Related Questions