Firebase data is "undefined" when it is not

10,335

I changed my code to this and now it works perfectly. The problem was that i was trying to get the value from the wrong child. Thanks for your help and patience!

var database = firebase.database().ref().child('codes');
var codeInput = document.getElementById('mainSearch');


database.child(codeInput).on('value', function(snap) {

          var data = snap.val();

          var listName = data.listname;
          var mainLanguage = data.mainLanguage;
          var secondLanguage = data.secondLanguage;
          var mainArray = data.mainArray;
          var secondArray = data.secondArray;

        }); 
Share:
10,335
Magnus
Author by

Magnus

Updated on June 04, 2022

Comments

  • Magnus
    Magnus almost 2 years

    I am trying to retrieve some data from my firebase database, but the value I get is "undefined".

    This is how I save the data to the database:

    var database = firebase.database();
    
            database.ref().push({
            mainArray: mainArray,
            secondArray: secondArray,
            listname: listName,
            mainLanguage: mainLanguage,
            secondLanguage: secondLanguage,
    
      }, function(error) {
      if (error){
    
        stopLoader();
    
        showSnackbar("An error has occured! Please try again later.");
    }
    

    This is how I read the data, but the value of listname is "undefined":

     var database = firebase.database().ref().child('codes');
        var codeInput = document.getElementById('mainSearch');
    
        database.on('value', function(snapshot) {
                if (!snapshot.hasChild(codeInput.value)) {
    
                    codeInput.value = "";
                    showSnackbar("A list with this code does not exist!<br><br>Please try another one.")
                }
                else {
    
                   var data = snapshot.val();
                   var listname = data.listname;
    
    
                   console.log(listname);
    
    
    
                }
            });
    

    This is the value I get from the database:

    This is the value I get from the database

    This is how the data is structured in the database:

    This is how the data is structured in the database: