Я развертываю облачные функции и получаю эту ошибку
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.
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);
});
});
Любая помощь будет высоко оценена.