Skip to content

Commit e474a32

Browse files
limexpandrewferlitsch
authored andcommitted
Fix wrong get_check_exists sample. No exception handling is required anymore (#2429)
* Fix wrong get_check_exists sample There are no more exceptions when no document found in DocumentReference.get() call. If the document does not exist at the time of the snapshot is taken, the snapshot’s reference, data, update_time, and create_time attributes will all be None and its exists attribute will be False. https://googleapis.github.io/google-cloud-python/latest/firestore/document.html#google.cloud.firestore_v1.document.DocumentReference.get Now issues googleapis/google-cloud-python#4530 and googleapis/google-cloud-python#4531 are resolved * removed unused exceptions import
1 parent c438ba1 commit e474a32

File tree

1 file changed

+3
-4
lines changed

1 file changed

+3
-4
lines changed

firestore/cloud-client/snippets.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
from time import sleep
1616

1717
from google.cloud import firestore
18-
import google.cloud.exceptions
1918

2019

2120
def quickstart_new_instance():
@@ -217,10 +216,10 @@ def get_check_exists():
217216
# [START get_check_exists]
218217
doc_ref = db.collection(u'cities').document(u'SF')
219218

220-
try:
221-
doc = doc_ref.get()
219+
doc = doc_ref.get()
220+
if doc.exists:
222221
print(u'Document data: {}'.format(doc.to_dict()))
223-
except google.cloud.exceptions.NotFound:
222+
else:
224223
print(u'No such document!')
225224
# [END get_check_exists]
226225

0 commit comments

Comments
 (0)