javascript - 我不断收到此错误 : Cannot modify a WriteBatch that has been committed

标签 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);
}


}

...................................................... ...................................................... ....

最佳答案

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

您可以通过多种方式处理这些问题,例如用 promise 链包装它或创建回调。 对于回调,请在 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);
    }
}

关于javascript - 我不断收到此错误 : Cannot modify a WriteBatch that has been committed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67246088/

相关文章:

javascript - 向 onCreate 用户 firebase 云函数添加自定义声明

javascript - 没有 CSS 转换的翻转文本

javascript - 使用 Bower 向 CKEditor 添加额外的插件

javascript - 更好的 jQuery Ajax 错误信息

node.js - 为特定模块编写测试时如何使用绝对导入路径

javascript - React.js Firebase 和 Firepad 组件

javascript - 检测鼠标光标类型

node.js - Node 摩卡测试用例未运行

node.js - 带回调和不带回调的 collection.find 有什么区别?

firebase - 找不到插件项目:firebase_core_web