【问题标题】:Retrieve the document ids of a nested subcollection in Firestore在 Firestore 中检索嵌套子集合的文档 ID
【发布时间】:2021-01-17 07:30:58
【问题描述】:

我的 Firebase Firestore 中有以下结构:

是否可以仅查询第二张图片中间列中显示的文档 ID?这是我当前的查询,在我看来这是最合乎逻辑的方式。 console.log 返回成功信息,但数组为空:

async function getListofPosts() {
    try{
        return await useFireStore
        .collection('chats')
        .doc(`${postId}`)
        .collection('chat')
        .get().then(res => {
            let theMessageList = [];
            res.forEach(doc => {
                theMessageList.push(doc.data(), doc.id )
            });
            setMessageList(theMessageList);
        });
    }
    catch {
        console.log('error on getListofPosts')
    }
    finally {
        console.log('getListofPosts worked')
    }
}

非常感谢任何帮助。 干杯,马特

【问题讨论】:

    标签: firebase google-cloud-firestore


    【解决方案1】:

    相关问题 - Firestore Cloud Function empty collection

    您的集合 chatschat 下的文档似乎是空的(文档 ID 以斜体显示)。所以要解决这个问题,你应该使用collectionGroup 查询。

    return await useFireStore
            .collectionGroup('chat')
            .get().then(res => {
                let theMessageList = [];
                res.forEach(doc => {
                    theMessageList.push(doc.data(), doc.id )
                });
                setMessageList(theMessageList);
    });
    

    在使用 collectionGroup 查询之前,我建议您阅读此查询,它的限制来自这里 - https://firebase.google.com/docs/firestore/query-data/queries#collection-group-query

    【讨论】:

    • 非常感谢,穆图。我没有意识到 Firestore 认为这些文档是空的,感谢您指出这一点。上面的代码仍然返回一个空数组,但我现在明白为什么了。我有解决方案时会发布。
    • 太棒了!是的,请在找到解决方案后更新一次。它会帮助观众!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-23
    相关资源
    最近更新 更多