【问题标题】:Firebase function onUpdate doesnt update another document "Cannot read property 'update' of undefined "Firebase 函数 onUpdate 不更新另一个文档“无法读取未定义的属性‘更新’”
【发布时间】:2021-09-04 07:15:57
【问题描述】:

这是我的代码:

  exports.updateCourseName = functions.firestore
    .document('courses/{courseId}')
    .onUpdate((change, context) => {
      const newValue = change.after.data().name;
      const previousValue = change.before.data().name;
      const course1Id = context.params.courseId;
      if(newValue != previousValue){
        console.log("veränderung!")
        return admin.firestore().collection("sets").where("course.id", "==", course1Id)
        .get()
        .then((querySnapshot) => {
          if (!querySnapshot.empty) {
                querySnapshot.docs[0].course.update({name: newValue})
          }
        })
      }
    });
    

这是我的火力基地结构:

代码运行时出现错误:TypeError: Cannot read property 'update' of undefined .但是在我更改“课程”中的名称后(这里是名称“dergerat”,名称在集合/{set_id}/course/name 中没有更改。更改名称后我得到日志“veränderung!”所以这个部分工作正常。 退货部分有问题吗?还是我做错了什么?

【问题讨论】:

    标签: javascript node.js firebase google-cloud-firestore google-cloud-functions


    【解决方案1】:

    在做

    querySnapshot.docs[0].course.update({author : newValue})
    

    无法工作。

    使用querySnapshot.docs[0],您将获得QueryDocumentSnapshot,并且该对象没有course 属性。 update() 方法是 DocumentReference 的方法,因此您应该按照以下几行进行操作:

    exports.updateCourseName = functions.firestore
        .document('courses/{courseId}')
        .onUpdate((change, context) => {
            const newValue = change.after.data().name;
            const previousValue = change.before.data().name;
            const course1Id = context.params.courseId;
            if (newValue != previousValue) {
                console.log("veränderung!")
                return admin.firestore().collection("sets").where("course.id", "==", course1Id)
                    .get()
                    .then((querySnapshot) => {
                        if (!querySnapshot.empty) {
                            return querySnapshot.docs[0].ref.update({ "course.name": newValue })
                        } else {
                            return null;
                        }
                    })
            } else {
              return null;
            }
        });
    

    我们假设查询只返回一个文档,因此我们可以使用docs[0] 来获取与set 文档对应的QueryDocumentSnapshot。然后我们使用QueryDocumentSnapshotref 属性并更新the field in the nested object


    PS:另请参阅answer 对您之前的问题以及在您的云函数中返回由update() 方法返回的承诺的重要性。

    【讨论】:

      【解决方案2】:

      那里的文档引用似乎有问题。你可以试试下面的代码:

      exports.updateCourseName = functions.firestore
      .document('courses/{courseId}')
      .onUpdate((change, context) => {
        const newValue = change.after.data().name;
        const previousValue = change.before.data().name;
        const course1Id = context.params.courseId;
        if(newValue != previousValue){
          console.log("veränderung!")
          const setsRef = admin.firestore().collection("sets").where("course.id", "==", course1Id)
          return setsRef.get().then((querySnapshot) => {
            if (!querySnapshot.empty) return null
            const setId = querySnapshot[0].id
            return admin.firestore().collection("sets").doc(setId).update({"course.name": newValue}).then(() => {
                console.log("Data updated")
                return null
            })
          })
        }
      });
      
      

      查询后,您可以轻松获得集合(或集合)的 ID。如果有多个集合可能包含相同的集合 ID,您可能必须使用 Batch Writes。上面的代码只会更新课程名称,但您可以类似地更新其他字段。

      【讨论】:

        猜你喜欢
        • 2021-02-27
        • 2019-12-20
        • 2020-05-06
        • 2018-10-30
        • 2018-04-01
        • 2020-03-26
        • 2018-09-13
        相关资源
        最近更新 更多