Skip to content

Instantly share code, notes, and snippets.

@MiroHibler
Last active June 26, 2020 10:48
Show Gist options
  • Save MiroHibler/4740915 to your computer and use it in GitHub Desktop.
Save MiroHibler/4740915 to your computer and use it in GitHub Desktop.
Firebase: Get the last item in a list. This snippet retrieves only the last item in a list.
// Run test @ http://jsfiddle.net/FNZKS/
function makeList(ref) {
var fruits = ["banana", "apple", "grape", "orange"];
for (var i = 0; i < fruits.length; i++) {
ref.push(fruits[i]);
}
}
function getLastFromList(ref, cb) {
ref.limit(1).once("child_added", function (snapshot) {
cb(snapshot.val());
});
}
// Running this should popup an alert with "orange".
function go() {
var testRef = new Firebase("https://example.firebaseIO-demo.com/");
makeList(testRef);
getLastFromList(testRef, function (val) {
alert(val);
});
}
@nsiddiqui248
Copy link

Sorry to bother you,
as I'm a beginner it's difficult for me to understand your snippet. Can you please provide me the code with more clarification.

@anshcena
Copy link

this.firebase.list('listname', ref => ref.limitToLast(1)).valueChanges()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment