When I query a collection for a document that doesn't exist, I'm still returned a non-nil object, which crashes when I try to call documentSnapshot?.data()
.
The error returned is "Document '<FSTDocumentKey: XXXXXX>' doesn't exist. Check document.exists to make sure the document exists before calling document.data.'"
This is how I handle if there is a snapshot or not.
You can use .exists only for DocumentSnapshot. You can't use it for QuerySnapshot because QuerySnapshot actually is a multiple DocumentSnapshot.
I've found this can be solved with a nifty little guard statement:
Here's a working example of this check:
I don't know why you have to call
.exists()
, and the document isn't just nil, but this is either part of the learning curve, or an artifact of a Beta SDK