Firestore orderBy Timestamp DESC

zip*_*pax 3 javascript sorting react-native google-cloud-firestore

我正在尝试按时间戳降序显示 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) });
                }
            }); 
Run Code Online (Sandbox Code Playgroud)

先前代码的结果是 id ASC 的数据顺序,我也像这样从 Firebase 控制台完成 INDEXING:

在此处输入图片说明

我希望我清楚地解释了这个问题。谢谢

zip*_*pax 5

在我朋友的帮助下,我们提出了这个解决方案并使代码按预期工作:

打算分享这个:

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 });
            }
        });
Run Code Online (Sandbox Code Playgroud)