The Code (Cont.): Output All


ref.on( "value", snapshot => {
The JavaScript arrow function => allows us to write shorter function syntax.

snapshot.forEach( child => {
The method child() gets another DataSnapshot for the location at the specified relative path. Passing a relative path to the child() method of a DataSnapshot returns another DataSnapshot for the location at the specified relative path. The relative path can either be a simple child name (for example, "Ash") or a deeper, slash-separated path (for example, "users/3/name"). If the child location has no data, an empty DataSnapshot (that is, a DataSnapshot whose value is null) is returned.

  Showing all users

out += "ID: " + child.key;
The last token in a database location is considered its key. For example, 3 is the key for the /users/3/ node. Accessing the key on any DataSnapshot will return the key for the location that generated it. However, accessing the key on the root URL of a database will return null.

out += child.key + ": " + child.val();
The method val() extracts a JavaScript value from a DataSnapshot. Depending on the data in a DataSnapshot, the val() method may return a scalar type (string, number, or boolean), an array, or an object. It may also return null, indicating that the DataSnapshot is empty (contains no data).
<script>
 //
 // Show all users.
 //
 function showAll( ) {
  var ref = firebase.database( ).ref( 'users/' );
  ref.on( "value", snapshot => {
   var out = "";
   snapshot.forEach( child => {
    out += "ID: " + child.key;
    var ref1 = firebase.database( ).ref( 'users/' + child.key );
    ref1.on( "value", snapshot => {
     snapshot.forEach( child => {
      out += child.key + ": " + child.val( );
     } )
    } )
   } )
   document.getElementById( 'result' ).innerHTML = out;
  } )
  return( false );
 }

</script>





      I will never forget my Granddad’s last words to me just before he died.    
      “Are you still holding the ladder?”