Vue Firestore 查詢。Firestore 更新數據時如何避免重複?

朱利安

在 ionic vue 應用程序中,我想顯示來自 Firebase Firestore 的一些數據。

在 Firestore 中,我有一個households帶有子集合的集合entries用戶可以是一個家庭的成員,我想顯示她所屬的所有家庭的所有條目。

我有一個數組entries在我的data

data() {
  return {
    entries: [],
  };
},

我通過對 firebase 進行三個嵌套查詢來填充這個數組:

  1. 獲取當前用戶
  2. 獲取與當前用戶關聯的所有家庭
  3. 獲取當前用戶所有家庭的所有條目
beforeCreate: function () {
  auth.onAuthStateChanged((user) => {
    if (user) {
      // Get current uid
      let uid = user.uid;

      // Loop trough all households of user
      let userHouseholdIds = [];
      db.collection("households")
        .where("members", "array-contains", uid)
        .onSnapshot((snapshotChange) => {
          snapshotChange.forEach((doc) => {
            userHouseholdIds.push(doc.id);
          });

          userHouseholdIds.forEach((householdId) => {
            // Get household name
            db.collection("households")
              .doc(householdId)
              .get()
              .then((value) => {
                let householdId = value.id;
                let householdName = value.data().name;

                // Get household entries
                db.collection("households")
                  .doc(householdId)
                  .collection("entries")
                  .onSnapshot((snapshotChange) => {
                    snapshotChange.forEach((doc) => {
                      let newDoc = doc.data();
                      newDoc["id"] = doc.id;
                      newDoc["householdName"] = householdName;
                      newDoc["householdId"] = householdId;
                      this.entries.push(newDoc);
                    });
                  });
              });
          });
        });
    }
  });

雖然我不確定這是否是要走的路,但這按預期工作(到目前為止)。

但是,當我直接在 Firestore 控制台e1中對家庭中的條目進行更改時,前端中的h1每個條目h1都會重複。

我想,this.entries = []在 Firestore 的更新進來之前,我需要一個地方,但我不知道在哪裡。

我在正確的軌道上嗎?我需要修改什麼以避免重複?非常感謝!

2月30日

如果您必須使用“onSnapshot”檢查 change.type

db
.collection(...)
.where(...)
.onSnapshot((snapshotChange) => {
    snapshotChange.docChanges().forEach(change => {
        if (change.type === 'added') {
          this.entries.push()
        } else if (change.type === 'modified') {
          this.entries.splice()
        } else if (change.type === 'removed') {
          this.entries.splice()
        }
      })
})

如果沒有,請嘗試使用 get()

db
.collection(...)
.where(...)
.get()

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何查詢規範化的 Firestore 數據庫

Cloud Firestore:查詢數組的嵌套映射

如何使用flutter查詢firestore中的子集合?

如何在react js中查詢firestore v9數據

Flutter Firestore :: 如何按地圖內的字段/值對 Firestore 查詢進行排序?

您如何根據查詢偵聽 Firestore 集合中的更改?

如何根據對文檔本身中的值的操作來查詢 Firestore

在 Android Studio Firestore 中獲取查詢時出現錯誤

如何查詢集合中的多個文檔(Firestore/Flutter)

對於天數可變的事件,如何構建 Firestore 集合以查詢每天的標籤?

如何編寫 firestore 規則來查詢另一個集合,其中值等於某個值

flutterfire firestore 中的字符串匹配查詢

Firestore 使用子集合查詢集合

Firebase Firestore:使用邏輯 OR 和分頁查詢

需要從 Firestore 的多個集合中查詢

使用 Flutter 使用 OR 運算符查詢 Firestore 集合

如果在 JavaScript 中未找到任何內容,則返回 Firestore 查詢

Firestore 查詢 (Web v9) 不返回任何結果

組合多個 Firestore 查詢以獲得特定結果(帶分頁)

Vue Firebase / Firestore重复项

Vue和Firestore数据绑定

Vue 3 与 Firestore 集成的问题

如何檢查firestore kotlin中是否存在文檔?

如何使Vue等待Firestore完成下载收藏集?

如何使用 Vue.js 组件从 Firestore 中删除图像

Firebase Cloud Firestore错误+ Vue + Vuex(firebase.firestore.QuerySnapshot)

如何使用查爾斯代理檢查 Firestore 網絡流量?

在 Firestore (Vue) 中使用 Firebase uid

编辑电子邮件Firestore Vue