Reputation: 586
I'm trying to get the id of the first child in my database, but it returns "undefined". Below is my code, and how my data is structured:
var database = firebase.database().ref();
var postsRef = database.child("posts");
postsRef.orderByChild("date").limitToFirst(1).once("value", function(snapshot) {
console.log(snapshot.ref.parent);
});
And this is how my data is structured:
posts
1
date
title
alias
timestamp
2
date
title
alias
timestamp
3
date
title
alias
timestamp
Upvotes: 2
Views: 3497
Reputation: 675
var postsRef = database.child("posts");
postsRef.orderByChild("date").limitToFirst(1).once("value", function(snapshot) {
console.log(snapshot.key);
});
https://firebase.google.com/docs/reference/js/firebase.database.Reference#limitToFirst https://firebase.google.com/docs/reference/js/firebase.database.Reference#key
Upvotes: 1
Reputation: 493
var snapshotKey = snapshot.key;
var snapshotData = snapshot.val();
Upvotes: 1