Speed up development with full-stack environments for every branch.

Learn More

Get the first item in a list in Firebase [JavaScript]

Forked from Detecting if data exists in Firebase.

230 Runs 1711 Views 320 Copies
Saved

Saved

sundip 66

sundip
published 4 years ago

<html>
  <head>
  
    <!-- Installing Firebase from CDN -->
    <script src='https://cdn.firebase.com/v0/firebase.js'></script>
    
    <script type="text/javascript">
    /* Recipe code to check if data exists in Firebase */
    
      function makeList(ref) {
        var fruits = ["banana", "apple", "grape", "orange"];
        for (var i = 0; i < fruits.length; i++) {
          ref.push(fruits[i]);
        }
      }
       
      function getFirstFromList(ref, cb) {
        ref.startAt().limit(1).once("child_added", function(snapshot) {
          cb(snapshot.val());
        });
      }
       
      // Running this should popup an alert with "banana".
      function go() {
        var testRef = new Firebase("https://example.firebaseIO-demo.com/");
        makeList(testRef);
        getFirstFromList(testRef, function(val) {
          alert(val);
        });
      }
      
    </script>
    <link rel="stylesheet" href="runnable.css" />
  </head>
  <body>
    <h1>Get the first item in a list in Firebase</h1>
    <p><a href="#" onclick="go();">Click here</a> to get the first item</p>
  </body>
</html>
Please login/signup to get access to the terminal.

Your session has timed out.

Dismiss (the page may not function properly).