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:
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 });
}
});