Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/react-native/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Firebase 如何通过Firestore中的引用字段连接两个集合中的数据?_Firebase_React Native_Google Cloud Firestore_React Hooks - Fatal编程技术网

Firebase 如何通过Firestore中的引用字段连接两个集合中的数据?

Firebase 如何通过Firestore中的引用字段连接两个集合中的数据?,firebase,react-native,google-cloud-firestore,react-hooks,Firebase,React Native,Google Cloud Firestore,React Hooks,我的firestore里有两个系列。一个叫主人,第二个叫独角兽。 所有者只有一个名称字段,而独角兽有一个名称和对所有者的引用。 我需要一个查询来返回如下所示的对象数组 unicorns = { id: 123, name: Dreamy, owner: { id: 1 name: John Cane } }

我的firestore里有两个系列。一个叫主人,第二个叫独角兽。 所有者只有一个名称字段,而独角兽有一个名称和对所有者的引用。 我需要一个查询来返回如下所示的对象数组

unicorns = { id: 123,
                name: Dreamy,
                owner: { id: 1
                         name: John Cane
                       }
               }
我的查询看起来像这样,但缺少一些我无法理解的内容

let unis = [];
      db
      .collection("unicorns")
      .get()
      .then((querySnapshot) => {
        querySnapshot.forEach((doc) => {
          let uni = {
            id: doc.id,
            name: doc.data().name,
            owner: doc.data().owner,
          };
          if (uni.owner) {
            doc
              .data()
              .owner.get()
              .then((res) => {
                uni.owner = {
                  id: res.id,
                  name: res.data().name,
                };
                unis.push(uni);
                
              })
              .then(() => {
                setUnicorns(unis);
              })
              .catch((err) => console.error(err));
          } else {
            unis.push(uni);
          }
        });
      })

当我设置Unicorns hook并尝试映射结果时,我无法获得所需的所有数据

您无法对字符串运行
.get()
方法。您必须向firestore运行单独的请求才能获取所有者文档。我建议在异步函数中使用for,如下所示

const db = admin.firestore()

//Declare the reference for collections before the loop starts
const ownersCollection = db.collection("owners")
const unicornsCollection = db.collection("unicorns")

const ownersData = {}

let unis = [];
unicornsCollection.get().then(async (querySnapshot) => {
    for (const doc of querySnapshot) {
        let uni = {
            id: doc.id,
            name: doc.data().name,
            //Assuming the owner field is the UID of owner
            owner: doc.data().owner,
        };
        if (uni.owner) {
            //Check if owner's data is already fetched to save extra requests to Firestore
            if (ownersData[uni.owner]) {
                uni.owner = {
                    id: ownersData[uni.owner].id,
                    name: ownersData[uni.owner].name,
                };
                unis.push(uni);
            } else {
                //User the ownersCollection Reference to get owner information
                ownersCollection.doc(uni.owner).get().then((ownerDoc) => {
                    uni.owner = {
                        id: ownerDoc.data().id,
                        name: ownerDoc.data().name,
                    };
                    ownersData[uni.owner] = {
                        id: ownerDoc.data().id,
                        name: ownerDoc.data().name,
                    }
                    unis.push(uni);
                }).then(() => {
                    setUnicorns(unis);
                }).catch((err) => console.error(err));
            }
        } else {
            unis.push(uni);
        }
    }
})
这是怎么回事?它获取所有unicorn文档,然后迭代检查文档是否有所有者的UID。如果是,则运行另一个请求到Firestore以获取该独角兽的所有者文档。如果你有任何问题,请告诉我


编辑:如果一个所有者有多个独角兽,那么您肯定不想一次又一次地获取同一所有者的数据。因此,在本地将其添加到对象中,并在向Firestore发出请求之前检查该所有者的数据是否已经获取。上面更新了相同的代码。

Hello@Reem,如果我的答案解决了您的问题,那么您可以通过单击勾选图标接受它,以便其他人知道问题已解决,否则请随时提问。