Display posts in descending posted order

前端 未结 18 944
南方客
南方客 2020-11-22 11:47

I\'m trying to test out Firebase to allow users to post comments using push. I want to display the data I retrieve with the following;

fbl.child         


        
18条回答
  •  遇见更好的自我
    2020-11-22 12:13

    To add to Dave Vávra's answer, I use a negative timestamp as my sort_key like so

    Setting

    const timestamp = new Date().getTime();
    const data = {
      name: 'John Doe',
      city: 'New York',
      sort_key: timestamp * -1 // Gets the negative value of the timestamp
    }
    

    Getting

    const ref = firebase.database().ref('business-images').child(id);
    const query = ref.orderByChild('sort_key');
    return $firebaseArray(query); // AngularFire function
    

    This fetches all objects from newest to oldest. You can also $indexOn the sortKey to make it run even faster

提交回复
热议问题