我正在尝试按时间戳降序显示 Firestore 中的数据,我按照文档进行操作,但似乎我做错了。
这是我的尝试:
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) });
}
});
先前代码的结果是 id ASC 的数据顺序,我也完成了 Firebase 控制台的索引,如下所示:
我希望我清楚地解释了这个问题。
谢谢
最佳答案
在我 friend 的帮助下,我们提出了这个解决方案,并使代码按预期工作:
打算分享这个:
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 });
}
});
关于javascript - Firestore orderBy Timestamp DESC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62474577/