无法修改已提交的WriteBatch。 (云功能)

时间:2019-11-27 14:30:07

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

仅当在短时间间隔内多次调用该函数时,有时是一次调用该函数,我才会收到此错误。

从其他响应中,建议返回null作为Firebase云功能的承诺。每次创建新文档时,都会在Firebase云功能内部调用此功能。有什么想法吗?

function createMilestone(path : String){

    var reg = "[^£]*£[^£]*";
    const uid = path.match(reg);
    if (uid){

    let ref = db.collection('caseStatus').doc(path).collection("0").doc("0")
    let ref1 = db.collection('caseStatus').doc(path).collection("0").doc("1")
    let ref2 = db.collection('caseStatus').doc(path).collection("0").doc("2")
    let ref3 = db.collection('caseStatus').doc(path).collection("0").doc("3")
    let ref4 = db.collection('caseStatus').doc(path).collection("0").doc("4")
    let ref5 = db.collection('caseStatus').doc(path).collection("0").doc("5")
    let ref6 = db.collection('caseStatus').doc(path).collection("0").doc("6")
    let ref7 = db.collection('caseStatus').doc(path).collection("0").doc("7")
    let ref8 = db.collection('caseStatus').doc(path).collection("0").doc("8")
    let ref9 = db.collection('caseStatus').doc(path).collection("0").doc("9")

       batch.set(ref ,arrayMilestones[0]);
       batch.set(ref1, arrayMilestones[1]);
       batch.set(ref2, arrayMilestones[2]);
       batch.set(ref3, arrayMilestones[3]);
       batch.set(ref4, arrayMilestones[4]);
       batch.set(ref5, arrayMilestones[5]);
       batch.set(ref6, arrayMilestones[6]);
       batch.set(ref7, arrayMilestones[7]);
       batch.set(ref8, arrayMilestones[8]);
       batch.set(ref9, arrayMilestones[9]);


       batch.commit().then(function () {
        console.log("imported milestones")
        return null

      });
    }

1 个答案:

答案 0 :(得分:2)

您应按如下所示返回Promises链

function createMilestone(path : String){

       //.....

       return batch.commit().then(function () {
        console.log("imported milestones")
        return null;
      });

    }

如果您不需要console.log(),只需

function createMilestone(path : String){

       //.....

       return batch.commit();

    }

如果仍然遇到问题,请向您的代码添加整个Cloud Function代码,而不仅仅是片段。