0
votes

I have a list of post. Each post contain firebase servervalue timestamp in createdAt.

let say, my post titles are 1,2,3,4,5. When i do this query.

const userPostsRef = database.ref(`userPosts/${uid}`)
                    .orderByChild('createdAt').limitToLast(10);`

And it still return 1,2,3,4,5. What I want is 5,4,3,2,1

I tried to navigative timestamp and it works. Why? and is that mean i have to use another child orderTimestamp with negative timestamp for reverse query?

Update json

I mean limitToLast();

userPosts
 sms|5797235fe618d33da2eb0ad3

  -Km2l4HHE3pONRnA2Akp
   createdAt: 1496859301233
   o: -1496859301233
   title: "2"

 -Km2l5QQRi1F66LJkM45
  createdAt: 1496859305916
  o: -1496859305916
  title: "3"

 -Km2uTa8tAe0G5cJ2U-B
  createdAt: 1496861764218
  o: -1496861764218
  title: "4"

 -Km2uUrfwvY90Im06kg4
  createdAt: 1496861769435
  o: -1496861769435
  title: "5"

orderByChild('createdAt')

enter image description here

orderByChild('o')

enter image description here

1
Please share a snippet of the JSON you're querying (as text, no screenshots). You can get this by clicking the "Export JSON" link in your Firebase Database console. - Frank van Puffelen
@FrankvanPuffelen updated. - vzhen
I think i got it. As @Mathew answer, firebase always returns in ascending order and because -10 is smaller than -5 so it return the newest post first. - vzhen

1 Answers

2
votes

It always returns in ascending order, it is up to you on the client to order it the way you want after retrieval. If you had 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 and you wanted 15,14,13,12,11 you would do .limitToLast(5) instead getting 11,12,13,14,15 and then ordering backwards on the client.