Firebase Firestore事务错误:无法修改已提交的WriteBatch [英] Firebase Firestore Transaction Error: Cannot modify a WriteBatch that has been committed

查看:57
本文介绍了Firebase Firestore事务错误:无法修改已提交的WriteBatch的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在云功能上进行部署,并出现此错误

I am deploying on cloud functions, and getting this error

Error: Cannot modify a WriteBatch that has been committed.
at WriteBatch.verifyNotCommitted (/user_code/node_modules/firebase-admin/node_modules/@google-cloud/firestore/build/src/write-batch.js:112:19)
at WriteBatch.update (/user_code/node_modules/firebase-admin/node_modules/@google-cloud/firestore/build/src/write-batch.js:299:14)
at Transaction.update (/user_code/node_modules/firebase-admin/node_modules/@google-cloud/firestore/build/src/transaction.js:225:33)
at transaction.get.then (/user_code/index.js:22:40)
at process._tickDomainCallback (internal/process/next_tick.js:135:7)

下面是我的代码,我正在使用事务在Firestore上进行更新和删除.

Below is my code, I am using transaction to update and delete on Firestore.

    const functions = require('firebase-functions');

 const admin = require('firebase-admin');
 admin.initializeApp();
   const firestore = admin.firestore();

  exports.updateUserSize = functions.auth.user().onDelete((user) => {
var userDocRef = firestore.collection("users").where('uid', '==', 
 user.uid).limit(1);

return firestore.runTransaction(function(transaction) {
    return transaction.get(userDocRef).then((snapshot) => {
        snapshot.forEach(userDoc => {
            if (!userDoc.exists) {
                throw "userDoc does not exist!";
            }
            console.log("user_uid: " + userDoc.data().uid + " | instanceDocId: " + userDoc.data().instance);

            var instanceDocRef = firestore.collection("utils").doc(userDoc.data().instance);
            return transaction.get(instanceDocRef).then((snapshot) => {
                console.log("snapshotn: " + snapshot.data().user_size);
                var newUserSize = snapshot.data().user_size - 1;
                transaction.update(instanceDocRef, { user_size: newUserSize });
            });
        });
    });    

}).then(function() {
    console.log("Transaction successfully committed!");
}).catch(function(error) {
    console.log("Transaction failed: ", error);
});
 });

任何帮助将不胜感激.

推荐答案

这是通过使用for循环解决的,如@Renaud Tarnec在评论中所述.

This was solved by using a for loop, as mentioned by @Renaud Tarnec in the comments.

我认为您的问题来自以下事实:您将transaction多次返回到forEach循环中.同样,您在执行transaction.update(instanceDocRef, { user_size: newUserSize });时也不会返回事务.谨建议您详细研究文档: https://firebase. google.com/docs/firestore/manage-data/transactions

I think your problem comes form the fact that you are returning several times the transaction into the forEach loop. Also you don't return the transaction when doing transaction.update(instanceDocRef, { user_size: newUserSize });. I would kindly suggest that you study into detail the documentation: https://firebase.google.com/docs/firestore/manage-data/transactions and https://firebase.google.com/docs/reference/js/firebase.firestore.Transaction. With the forEach loop I think that you are not following the constraint: "A transaction consists of any number of get() operations followed by any number of write operations such as set(), update(), or delete(). "

这篇关于Firebase Firestore事务错误:无法修改已提交的WriteBatch的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆