【问题标题】:Vue and firebase Uncaught TypeError: snapshot.forEach is not a functionVue 和 firebase 未捕获类型错误:snapshot.forEach 不是函数
【发布时间】:2020-08-14 10:57:45
【问题描述】:

我正在尝试通过以下代码读取我的 Vue 应用程序上的 Firestore 数据库:

mounted() {
    db.collection("tete-est")
      .doc("inqueritos")
      .onSnapshot(snapshot => {
        snapshot.forEach(doc => {
            this.inquerito.push({
              codigo: doc.data().a_codAgr,
              data: doc.data().a_data,
              inquiridor: doc.data().a_nomeInq,
              nome: doc.data().c1_nomeChefe
            });
        });
      });

  },

我收到了这个错误:

Uncaught TypeError: snapshot.forEach is not a function
[enter image description here][1]

我的数据库如下所示:

谁能帮忙?

【问题讨论】:

  • 你到底想读什么? inqueritos 集合中的所有文档?

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


【解决方案1】:

查看您数据库的屏幕截图,inqueritos 是一个(根)集合,tete-est 是您的 Firebase 项目名称。

所以如果你想查询inqueritos集合中的所有文档,你应该这样做:

mounted() {
    db.collection("inqueritos")
      .onSnapshot(snapshot => {
        snapshot.forEach(doc => {
            this.inquerito.push({
              codigo: doc.data().a_codAgr,
              data: doc.data().a_data,
              inquiridor: doc.data().a_nomeInq,
              nome: doc.data().c1_nomeChefe
            });
        });
      });

  },

【讨论】:

【解决方案2】:

您正在对文档引用调用 onSnapshotforEach 方法在 querySnapshot 上可用,这是对集合引用的查询结果。

查看文档:https://firebase.google.com/docs/firestore/query-data/listen#listen_to_multiple_documents_in_a_collection

你会找到答案的:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-06
    • 1970-01-01
    • 2019-05-17
    • 2021-11-15
    • 2022-11-19
    • 1970-01-01
    • 1970-01-01
    • 2021-11-28
    相关资源
    最近更新 更多