首页 > 解决方案 > 我正在尝试访问特定行,但在 Vue 中出现错误

问题描述

错误是Uncaught TypeError: snapShotChnaged.forEach is not a function。我需要一个特定的行,这就是我使用文档的原因。

getAllDonations() {
    firestore
      .collection('donations')
      .doc(this.$route.params.id)
      .onSnapshot((snapShotChnaged) => {
          this.donations = []
          snapShotChnaged.forEach((donationDoc) => {
              this.donations.push({
                  id: donationDoc.id,
                  name: donationDoc.data().name,
                  surname: donationDoc.data().surname,
                  class: donationDoc.data().class,
                  amount: donationDoc.data().amount
              })
          });
      });
},

标签: javascriptfirebasevue.jsvuejs2vue-router

解决方案


您正在调用onSnapshot(): DocumentReference( ),因此(即您的对象)firestore.collection('donations').doc()没有forEach()方法。DocumentSnapshotsnapShotChnaged

一个人会在for sforEach()querySnapshot返回值上使用。onSnapshot()CollectionReference

您需要执行以下操作:

firestore
  .collection('donations')
  .doc(this.$route.params.id)
     .onSnapshot(doc => {
        const docData = doc.data());
        // ....
  });

推荐阅读