Reputation: 43
Here is my code that retrieves last value
Firebase
.database()
.ref('comment/commentHistory/' + postId)
.limitToLast(1)
.once('value', (snapshot) => {
// Stuff
});
Upvotes: 3
Views: 1694
Reputation: 3738
Are your postIds sequential? If they are, you can use the orderByKey clause. If they're not, you'll need to add some kind of counter or timestamp.
Firebase
.database()
.ref('comment/commentHistory')
.orderByKey
.limitToLast(2)
.once('value', (snapshot) => {
// take the last item with the lowest key in the snapshot
});
If your data isn't sequential, you'll need to add a timestamp as a child to your post.
Then you could use the orderByChild clause:
Firebase
.database()
.ref('comment/commentHistory')
.orderByChild('timestamp')
.limitToLast(2)
.once('value', (snapshot) => {
// take the last item with the lowest timestamp in the snapshot
});
Upvotes: 1