【问题标题】:Firebase Collection Group Query in VueJSVueJS 中的 Firebase 集合组查询
【发布时间】:2021-08-04 04:36:57
【问题描述】:

created() {
              firebase.auth().onAuthStateChanged((user) => {
            if (user) {
                this.userID = user.uid;
                console.log(this.userID);
            } else {
                console.log('User is not logged in.');
                this.$router.push('/login');
            }
         });
  },
  data() {
    return {
      pantry: [],
      userID: '',
    };
  },
  methods: {
    getCategoryDataFromFirebase() {
      const db = firebase.firestore();
      db.collectionGroup("pantry")
        .where("UserID", "==", this.userID)
        .get()
        .then((querySnapshot) => {
          querySnapshot.forEach((doc) => {
            this.pantry.push(doc.data());
          });
        })
        .catch((error) => {
          console.log("Error getting documents: ", error);
        });
    },
  },
};

我正在尝试执行一个集合组查询,我在其中获取我的 firestore 数据库中与创建它们的用户 ID 相关联的所有列表。我能够在创建的钩子上获取用户 ID 并将其存储在我的 this.userID 中。然后我想在我的 getCategoryDataFromFirebase 函数上使用该 ID 来执行集合组查询。 建议在控制台中创建集合组查询并从 firebase 获取 SDK 错误,然后使用控制台中的附加链接自动创建适当的规则,而不是像我那样手动创建。现在我在想我一定没有正确引用该组,因为我无法从我的 Firestore 中取回任何数据。我试图通过主要的“食品储藏室”创建组,但我认为我可能需要进一步深入数据库或以另一种方式设置查询。我将不胜感激任何可以提供的指导。我也附上了我的firestore的视图以供参考。我正在尝试获取每个类别(即水果、蔬菜等)中的所有用户 ID。

【问题讨论】:

    标签: javascript firebase vue.js google-cloud-firestore


    【解决方案1】:

    当您使用db.collectionGroup("pantry") 时,您正在读取所有名为pantry 的集合。在你的代码中那只是一个单一的顶级集合。

    如果要读取所有Vegetables集合,需要查询db.collectionGroup("Vegetables")

    【讨论】:

    • 好的,谢谢弗兰克!这就说得通了。我认为我不了解 collectionGroup 的运作方式。我的操作前提是它会搜索 pantry 下的所有子文档。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-20
    • 1970-01-01
    • 2023-03-31
    • 1970-01-01
    • 1970-01-01
    • 2019-03-27
    • 1970-01-01
    相关资源
    最近更新 更多