splunk splunk - 1 year ago 131
Javascript Question

Limit an ordered list in Firebase

I have this:

name | age
Anne | 28
Carl | 20
Sarah | 13
Lucy | 56
John | 22
Mark | 44

How do I retrieve the name of the 3 youngest people with Firebase?

I tried with:

return firebase.database().ref('myUsers').orderByChild("age").startAt().limit(3);

but I get an error:

Supplied parameters do not match any signature of call target.

Answer Source

Data in firebase is ordered lexicographically, so you'll have to get the last of them.

return firebase.database().ref('myUsers').orderByChild("age").limitToLast(3);

Docs with example: https://firebase.google.com/docs/reference/js/firebase.database.Query#limitToLast

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download