我不断收到此错误:无法修改已提交的 WriteBatch

时间:2021-04-24 18:22:13

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

我正在尝试提交批处理,但我不断收到此错误,但无法弄清楚我哪里出错了,我尝试将批处理移到某些功能之外但仍然没有进展,这是我的代码,我不确定我是什么我失踪了,我不断收到此错误:

scheduledFunction
Error: Cannot modify a WriteBatch that has been committed.
    at WriteBatch.verifyNotCommitted (/workspace/node_modules/@google-cloud/firestore/build/src/write-batch.js:117:19)
    at WriteBatch.update (/workspace/node_modules/@google-cloud/firestore/build/src/write-batch.js:313:14)
    at /workspace/index.js:152:48
    at QuerySnapshot.forEach (/workspace/node_modules/@google-cloud/firestore/build/src/reference.js:748:22)
    at step2 (/workspace/index.js:91:39)
    at runMicrotasks (<anonymous>)
    at processTicksAndRejections (internal/process/task_queues.js:97:5) 

这是我的代码

async function updateBets() {
var marketRef = db.collection('matches');
var snapshot = await marketRef.where('matchStatus', '==', 'FINISHED').get();





if (snapshot.empty) {
    console.log('No matching documents.');
    return;


}



console.log('I found documents');


snapshot.forEach(doc => {



    if (doc.data().matchId == null) {


        //console.log('document with error" ' + doc.id);


    }



    step2();



    async function step2() {


        if (doc.data().matchId != null) {

            var query = doc.data().matchId.toString();
            // console.log('here is match ID' + query);


            var marketRef2 = db.collection('markets');
            var snapshot2 = await marketRef2.where('marketId', '==', query).get();



            snapshot2.forEach(doc2 => {


                if (doc2.data().marketTitleId == 'FULL_TIME_RESULT') {



                    var a = doc.data().homeTeamScore;
                    var b = doc.data().awayTeamScore;



                    var winnerIndex;


                    if (a > b) {


                        winnerIndex = 0;

                        var resultIndex = ['WINNER', 'LOSER', 'LOSER'];

                        var docName = `${doc.data().matchId}` + '000' + '1';


                        var sfRef = db.collection('markets').doc(docName);
                        batch5.update(sfRef, {
                            results: resultIndex
                        });



                    } else if (a == b) {


                        winnerIndex = 1;

                        var docName = `${doc.data().matchId}` + '000' + '1';


                        var resultIndex = ['LOSER', 'WINNER', 'LOSER'];

                        var sfRef = db.collection('markets').doc(docName);
                        batch5.update(sfRef, {
                            results: resultIndex
                        });



                    } else if (a < b) {


                        winnerIndex = 2;

                        var docName = `${doc.data().matchId}` + '000' + '1';



                        var resultIndex = ['LOSER', 'LOSER', 'WINNER'];

                        var sfRef = db.collection('markets').doc(docName);
                        batch5.update(sfRef, {
                            results: resultIndex
                        });


                    }

                }



            })


        }




    }




});


try {
    await batch5.commit();
    console.log("im done with results");
} catch (err) {
    console.log('Mac! there was an error with results: ', err);
}


}

..................................... ………………………………………………………………………………………………………………………………………………………… ....

1 个答案:

答案 0 :(得分:0)

您的主函数是一个异步函数,它将继续异步运行所有方法,忽略任何内部函数。在尝试提交之前,您必须确保所有应用于批处理的作业都已添加,从本质上讲,这是一个竞争条件。

您可以通过多种方式处理这些问题,例如用承诺链包装它或创建回调。 对于回调,在您的 forEach

之前添加以下内容
var itemsProcessed = 0;

在 forEach 的末尾插入这个

    itemsProcessed++;
    if(itemsProcessed === array.length) {
      CommitBatch();
    }

然后将您的 batch.commit() 方法放在此回调函数中

function CommitBatch(){
    try {
        await batch5.commit();
        console.log("im done with results");
    } catch (err) {
        console.log('Mac! there was an error with results: ', err);
    }
}