letsCode
letsCode

Reputation: 3046

Getting Correct Child in Firebase Web

I have the following JS code.

  var database = firebase.database();
  database.ref().child('server/user-search').once('value', function(snapshot) {
    if (snapshot.exists()) {
      var content = '';
      snapshot.forEach(function(child) {
        var val = child.val();
        content += '<tr>';
        content += '<td>' + val.deviceCodename + '</td>';
        content += '</tr>';
      });
      $('#ex-table').append(content);
    }
  });

enter image description here

This is the layout I have. Its the child within user-search. Thanks for the help

Upvotes: 0

Views: 36

Answers (1)

Renaud Tarnec
Renaud Tarnec

Reputation: 83058

You have an extra node "0" between the "user-search" UID and the deviceXXX nodes.

If I make the assumption that this extra node is always "0", you should do something like the following:

    var database = firebase.database();
    database.ref().child('server/user-search').once('value', function (snapshot) {
        if (snapshot.exists()) {
            var content = '';
            snapshot.forEach(function (child) {
                var val = child.val()['0'];
                content += '<tr>';
                content += '<td>' + val.deviceCodename + '</td>';
                content += '</tr>';
            });
            $('#ex-table').append(content);
        }
    });

Upvotes: 2

Related Questions