【问题标题】:How to get value from multiple collection at a time from cloud firastore of firebase with react native? [closed]如何通过原生反应从 Firebase 的云 firastore 一次从多个集合中获取价值? [关闭]
【发布时间】:2020-10-06 10:46:45
【问题描述】:

enter image description here

我的 firebase firestore 设置是这样的,我想在同一页面中同时获取家人、朋友等其他集合的所有数据并将其显示为平面列表。

请帮忙解决。

【问题讨论】:

    标签: javascript firebase react-native google-cloud-firestore react-redux


    【解决方案1】:

    如果我没有误解您的问题,根据您的 Firestore 结构,以下代码应该可以帮助您从某个 subcollection 获取所有 documents

     return (dispatch) => {
      firebase.firestore().collection('user').doc('1')
       .collection('family').get()
       .then((querySnapshot) => {
         querySnapshot.forEach((doc) => {
           console.log(doc.id, " => ", doc.data());
         });
       });
     };
    

    上例中使用了子集合“family”。请注意,要使其正常工作,集合名称​​应该是可预测的。根据documentation,Firebase 目前不允许使用移动/网络客户端库检索集合列表。

    移动/网络客户端库无法检索集合列表。您应该只在受信任的服务器环境中将集合名称作为管理任务的一部分进行查找。如果您发现在移动/Web 客户端库中需要此功能,请考虑重构您的数据,以便子集合名称是可预测的。

    现在,如果您有兴趣跨多个子集合进行查询,您可能需要使用collection group queries。例如,您可以查询所有拥有friend 名为Johnusers

    var users = db.collectionGroup('friend').where('friendName', '==', 'John');
    users.get().then(function (querySnapshot) {
        querySnapshot.forEach(function (doc) {
            console.log(doc.id, ' => ', doc.data());
        });
    });
    

    请注意,userfriend 都是集合,John 将引用朋友子集合中的文档。

    要查找特定的 React Native for Firebase 示例,这是官方推荐的 documentation

    【讨论】:

    • 谢谢你的 sllopis,它对我有很大帮助..
    猜你喜欢
    • 2020-08-10
    • 2021-02-13
    • 1970-01-01
    • 2021-03-23
    • 2013-09-27
    • 1970-01-01
    • 2018-07-05
    • 2021-10-28
    • 2019-08-23
    相关资源
    最近更新 更多