How to tell if a Firestore operation succeeded or failed?

Viewed 594

I need to be able to tell if a set(), delete(), or update() operation on a document has succeeded before I do a transaction operation to increment or decrement a counter.

I've tried to print what set(), delete() and update() return, but it always just returns "seconds" and "nanos" whether or not the operation succeeded or not. I've tried to do operations on document IDs that don't exist, or collections that don't exist, but it always just returns the same thing with no indication if it did anything or not.

collection.("some_col").document("SoM3DoC").delete()
collection.("some_other_col").document("SoM30tH3RDoC").collection("some_col_ref").document("SoM3DoC").delete()
Then, ONLY if the above succeeded (the document existed and was deleted):
some_transaction(transaction, collection.("some_other_col").document("SoM30tH3RDoC")) # decrement a counter in this doc

I'm expecting that the operation methods would either throw an error if it couldn't complete the operation or return some message to indicate it but I can't seem to get any response. I even tried starting with some random collection like collection.("asdfsergreasg").document... but there's still no response.

1 Answers

The API documentation indicates what to expect from various operations, so you should use that as a reference. Some operations on documents and collections that don't exist don't yield errors. Some do. For example, calling get on a document that doesn't exist isn't an error, but the returned object will be clear that no document exists. However, calling update on a document that doesn't exist should raise an error.

Related