在单个Vue组件中从Firestore获取父文档和子文档数据

时间:2020-08-26 08:05:19

标签: firebase vue.js vuefire

我在Firestore中有一个父(组织)文档和多个子文档。我想根据是否在同一组件中单击了父级或子级来加载他的数据。

下面的代码有效,显示了数据,但未实时显示对子组织的更新(我必须重新加载才能看到它)。我猜是因为绑定了数组instanceQueue.addTaskToQueue(["getString", [1]]) // Error ✅ 而不是我实际用来显示数据的对象orgArray。有没有办法只绑定对象而不是整个数组?

org

1 个答案:

答案 0 :(得分:0)

我通过使用childOrg中的id并获取具有该id的数据来解决此问题,这样我就可以直接绑定数据对象。

firebase.auth().onAuthStateChanged((user) => {
      firestore.collectionGroup('people').where('userId', '==', user.uid).get().then((query) => {
        query.forEach((userRef) => {
          const orgRef = userRef.ref.parent.parent.id;
          this.orgRef = orgRef;
          if (this.$route.query.parent !== 'true') {
            firestore.collection('organisations').doc(orgRef).collection('childOrganisations').where('name', '==', this.$route.params.id)
              .get()
              .then((q) => {
                q.forEach((ref) => {
                  const orgId = ref.id;
                  const organisation = firestore.collection('organisations').doc(orgRef).collection('childOrganisations').doc(orgId);
                  this.$bind('org', organisation);
                });
              });
          } else {
            const organisation = firestore.collection('organisations').doc(orgRef);
            this.$bind('org', organisation);
          }
        });
      });
    }, (error) => {
      console.log(error);
    });