Skip to content

Instantly share code, notes, and snippets.

@guibot17
Forked from anantn/firebase_first_item.js
Last active September 15, 2015 19:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guibot17/41c8429d9dfc856d8c0c to your computer and use it in GitHub Desktop.
Save guibot17/41c8429d9dfc856d8c0c to your computer and use it in GitHub Desktop.
Firebase: Get the first item in a list. This snippet retrieves only the first item in a list.
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);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment