【问题标题】:Firebase function doesnt update, function returned undefinedFirebase 函数未更新,函数返回未定义
【发布时间】:2021-06-19 16:51:17
【问题描述】:

我正在尝试使用这个 firebase 功能来更新作者。

 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){

        admin
        .firestore().collection("set").where("course.id", "==", course1Id)
        .get()
        .then((querySnapshot) => {
          if (!querySnapshot.empty) {
         //       doc.data().author = newValue
                querySnapshot.docs[0].course
                .update({author : newValue
                })
          }
        })
        .catch((error) => {
            console.log("Error changing Author name in courses", error);
        });
      }
    })
    

我在日志中没有收到错误,但 Function returned undefined, expected Promise or value 我做错了什么?

【问题讨论】:

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


    【解决方案1】:

    您需要在所有异步工作完成后终止 Cloud Function,请参阅doc。对于后台触发的 Cloud Function(例如 Cloud Firestore function onUpdate trigger),您必须返回异步方法调用返回的整个 chain of promises

    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){
    
            return admin   // <== See the return here
            .firestore().collection("set").where("course.id", "==", course1Id)
            .get()
            .then((querySnapshot) => {
              if (!querySnapshot.empty) {
             //       doc.data().author = newValue
                    return querySnapshot.docs[0].course  // <== See the return here AND see below
                    .update({author : newValue
                    })
              } else  {
                   return null;   // <== See here: we indicate to the CF that it can be cleaned up
              }
            })
            .catch((error) => {
                console.log("Error changing Author name in courses", error);
                return null;
            });
          } else  {
            return null;   // <== See here: we indicate to the CF that it can be cleaned up
          }
        });
    

    但是,我不清楚你想做什么:

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

    course 到底是什么? update() 方法是 DocumentReference 的方法。

    你可能想做

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

    【讨论】:

      【解决方案2】:

      错误消息非常明确,那里:Function returned UNDEFINED, expected Promise or value

      您显示的不完整代码片段不会返回任何内容。试试:

      return admin
           .firestore().collection("set").where("course.id", "==", course1Id)
      ...etc, etc...
      

      【讨论】:

        猜你喜欢
        • 2018-03-28
        • 1970-01-01
        • 1970-01-01
        • 2018-12-20
        • 1970-01-01
        • 2018-12-05
        • 1970-01-01
        • 1970-01-01
        • 2020-10-22
        相关资源
        最近更新 更多