Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firestore orderBy Timestamp DESC

I'm trying to display the data from Firestore order by timestamp descending order, I follow the documentation but it seems that I did something wrong.

This is my try:

const outputSnapShot = {};

this.subscribe = firebase
  .firestore()
  .collection('orders')
  .where('restaurant_code', '==', this.state.restaurantCode)
  .orderBy('timestamp', 'desc')
  .onSnapshot((doc) => {
      doc.docs.map(function(documentSnapshot) {
        return (outputSnapShot[documentSnapshot.id] = documentSnapshot.data());
      });
      if (this._isMounted) {
        this.setState({ dataSource: Object.entries(outputSnapShot) });
      }
  }); 

the result from previous code is the data order by id ASC , Also I finish the INDEXING from Firebase console like so:

enter image description here

like image 374
zippax Avatar asked Nov 01 '25 09:11

zippax


1 Answers

With a help by my friend, we came with this solution and made the code work as we expected:

Going to share this:

this.subscribe = firebase
        .firestore()
        .collection('orders')
        .where('restaurant_code', '==', this.state.restaurantCode)
        .orderBy('timestamp', 'desc')
        .onSnapshot((docSnapshot) => {
            const dataSource = [];
            docSnapshot.forEach((doc) => {
                dataSource.push(doc.data());
            });
            if (this._isMounted) {
                this.setState({ dataSource });
            }
        });
like image 54
zippax Avatar answered Nov 02 '25 23:11

zippax



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!