0

I have a firebase database at this structure

DATA: { 
 DATE: {
  KEY: {
     NAME: "",
     AGE: "".
     ADDRESS: ""
     }
  } 
}

    var ref = database.ref('DATA');
    ref.orderByChild('AGE').equalTo(AGE).on('child_added',
    function (snapshot) {
      console.log(snapshot.val());
          if (snapshot.exists()) {
            var content = '';
            snapshot.forEach(function (data) {
                 var val = data.val();
                 console.log(data.val());
                 content += '<tr>';

                 if (val.AGE) {
                      content += '<td><strong>' + val.AGE + '</strong></td>';
                      } else {
                      content += '<td>-</td>';
                      }

I tried to console log the snapshot.val but it shows null. How can I do this? Thank you.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807

2 Answers2

0

try this:

console.log(snapshot.val().AGE);
norah
  • 69
  • 1
  • 10
0

Firebase queries work on a flat list of nodes, and the value you order/filter on must exist at a fixed location under each direct child node of the location you're querying.

If the KEY is always called KEY, you can query its AGE property with:

var ref = database.ref('DATA');
ref.orderByChild('KEY/AGE').equalTo(AGE)...

But if the KEY is actually a dynamic value, then the AGE is not in a fixed path for each child and you can't query on it.

In that case you'll have to restructure your data (or add an additional structure) for this use-case as shown in Firebase Query Double Nested and in Firebase query if child of child contains a value.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807