splunk
splunk

Reputation: 6805

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.

Upvotes: 3

Views: 14559

Answers (1)

Mathew Berg
Mathew Berg

Reputation: 28750

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

Upvotes: 11

Related Questions