【问题标题】:Cannot modify a WriteBatch that has been committed. (Cloud function)无法修改已提交的 WriteBatch。 (云功能)
【发布时间】:2019-11-27 14:30:07
【问题描述】:

只有在短时间内多次调用该函数时才会出现此错误,偶尔会调用一次。

从其他回复中,它建议返回 null 作为 Firebase 云函数的 Promise。每次创建新文档时,都会在 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

      });
    }

【问题讨论】:

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


【解决方案1】:

你应该返回 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 代码添加到您的代码中,而不仅仅是一个 sn-p。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-27
    • 2021-07-18
    • 1970-01-01
    • 1970-01-01
    • 2022-11-26
    • 2020-08-21
    相关资源
    最近更新 更多