Firebase函数未更新,函数返回未定义

ars1skjm  于 2023-06-07  发布在  其他
关注(0)|答案(2)|浏览(212)

我尝试用这个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我做错了什么?

kqlmhetl

kqlmhetl1#

当所有异步工作完成时,您需要终止Cloud Function,请参阅doc。在后台触发的云函数(例如,云Firestore函数onUpdate触发器)必须返回异步方法调用返回的整个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})

什么是courseupdate()方法是DocumentReference方法的一种。
你可能想做

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

piv4azn72#

错误消息非常明确,在那里:Function returned UNDEFINED, expected Promise or value
您显示的不完整代码片段不会返回任何内容。尝试:

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

相关问题