Get data from each firebase key with javascript

Asked

Viewed 153 times

0

I have the following registration at firebase:

inserir a descrição da imagem aqui

And I’m trying to get the values "Female", "Male" and "Mixed" of each record and in case it is "true" I mark a checkbox, but apparently my code is not validating record by record but all at once

My code (I haven’t implemented all conditions yet I’m testing with only one):

    var db = firebase.database().ref("modality");
    db.once("value", function(snapshot){
      snapshot.forEach(function(child){

        if ( child.val().female == "true" ){
          inputModalityFemale.checked = true;
        } else {
          inputModalityFemale.checked = false;
        }
        console.log(child.val().mixed);
        
      });
    });

Would anyone have any tips?

1 answer

0

The answer you get in the "snapshot" variable is not the data that comes from your database. In order for you to have access to the data, you have to perform the ". val()" function in the firebase response. It follows the amendments:

var db = firebase.database().ref("modality");
    db.once("value", function(snapshot){
      snapshot.val().forEach(function(child){

        if ( child.female == "true" ){
          inputModalityFemale.checked = true;
        } else {
          inputModalityFemale.checked = false;
        }
        console.log(child.mixed);

      });
    });

When executing the ". val()" function in the answer, then you have the array to be used in the iteration.

  • In this case using your suggestion I got : Uncaught Typeerror: snapshot.val(...). foreach is not a Function

  • if vc der console.log() in snapshot.val(), what appears there on the console?

  • logger.ts:84 [2019-05-26T13:25:57.238Z] @firebase/database: FIREBASE WARNING: Exception was thrown by user callback. Typeerror: snapshot.val(...). foreach is not a Function Uncaught Typeerror: snapshot.val(...). foreach is not a Function

Browser other questions tagged

You are not signed in. Login or sign up in order to post.