I'm trying to use firestore batch from the firebase-admin
Node.js library.
And I want to do some other stuff only if the batch write is succeeded. But I can't see any option to check if the batch was successfully committed or was rolled back.
I can't find any documentation regarding this from the official google api docs or the official firebase docs.
My Code:
import {getFirestore} from "firebase-admin/firestore";
const firestore = getFirestore();
const batch = firestore.batch();
batch.update(docRef, {"blah_blah_blah": true});
batch.set(docRef2, {"blah_blah_blah": false});
await batch.commit();
// ... if batch succeeded, do some other stuff
The general understanding with all functions that return a promise, such as batch.commit()
, is that the returned promise will become rejected if there is an error while performing the work asynchronously. You can use normal JavaScript error handling to find out if an async function returned a promise that was rejected when using async/await.
try {
await batch.commit();
}
catch (e) {
// something failed with batch.commit().
// the batch was rolled back.
console.error(e);
}
You can also use the catch function on the returned promise object if you are not using async/await.
If you need more help on javascript error handling with promises, I suggest doing some web searches on the topic. Firestore is not adding anything special here.