I am trying to create a Google Cloud function with my python script, but I keep getting an error. I am getting an error when trying to update a field in a firestore document with a nested dictionary. Here's my code snippet where it's happening:
driverDB = driverDB = db.collection('drivers')
driverList = []
for doc in driverDB.get():
driverList.append(doc.id)
for driver in range(0,len(driverList)):
routeDataDict = {
'route':routeByDriverDict[driver]
}
driverDB.document(driverList[driver]).update(routeDataDict)
The error I get is:
File "/layers/google.python.pip/pip/lib/python3.8/site-packages/google/cloud/firestore_v1/field_path.py",
line 272, in __init__ raise ValueError(error) ValueError: One or more components is not a string or is empty.
routeDataDict looks like this:
{'route': {'stop_1': {'name': 'Christa',
'address': '742 Evergreen Terrace',
'phone': 5555555555,
'email': 'test@test.com',
'has_reusables': False,
'notes': None,
'zone': 1,
'leg_url': '',
'seconds_until_arrival': 360.0,
'completed': False,
'num_containers_at_this_stop': 0},
'url': 'https://www.google.com/maps/dir/39.74,15.0757',
'pay': '$49.77',
'partner': 'burger_hut',
'date': datetime.datetime(2020, 11, 24, 1, 7, 1, 90282)}}
And driverList[driver] correctly prints out the string of the document ID, so the error seems to be in the dictionary.
The full error stack:
Traceback (most recent call last): File "/layers/google.python.pip/pip/lib/python3.8/site-packages/flask/app.py", line 2447, in wsgi_app response = self.full_dispatch_request() File "/layers/google.python.pip/pip/lib/python3.8/site-packages/flask/app.py",
line 1952, in full_dispatch_request rv = self.handle_user_exception(e) File "/layers/google.python.pip/pip/lib/python3.8/site-packages/flask/app.py",
line 1821, in handle_user_exception reraise(exc_type, exc_value, tb) File "/layers/google.python.pip/pip/lib/python3.8/site-packages/flask/_compat.py",
line 39, in reraise raise value File "/layers/google.python.pip/pip/lib/python3.8/site-packages/flask/app.py", line 1950, in full_dispatch_request rv = self.dispatch_request() File "/layers/google.python.pip/pip/lib/python3.8/site-packages/flask/app.py",
line 1936, in dispatch_request return self.view_functions[rule.endpoint](**req.view_args) File "/layers/google.python.functions-framework/functions-framework/lib/python3.8/site-packages/functions_framework/__init__.py", line 66, in view_func return function(request._get_current_object()) File "/workspace/main.py",
line 282, in routeCreatorMain driverDB.document('Lu2qHfOqtma48r7L2g4PsEDQizD2').update({'route':routeByDriverDict[driver]}) File "/layers/google.python.pip/pip/lib/python3.8/site-packages/google/cloud/firestore_v1/document.py",
line 284, in update batch, kwargs = self._prep_update(field_updates, option, retry, timeout) File "/layers/google.python.pip/pip/lib/python3.8/site-packages/google/cloud/firestore_v1/base_document.py",
line 232, in _prep_update batch.update(self, field_updates, option=option) File "/layers/google.python.pip/pip/lib/python3.8/site-packages/google/cloud/firestore_v1/base_batch.py",
line 126, in update write_pbs = _helpers.pbs_for_update( File "/layers/google.python.pip/pip/lib/python3.8/site-packages/google/cloud/firestore_v1/_helpers.py",
line 847, in pbs_for_update extractor = DocumentExtractorForUpdate(field_updates) File "/layers/google.python.pip/pip/lib/python3.8/site-packages/google/cloud/firestore_v1/_helpers.py",
line 798, in __init__ super(DocumentExtractorForUpdate, self).__init__(document_data) File "/layers/google.python.pip/pip/lib/python3.8/site-packages/google/cloud/firestore_v1/_helpers.py",
line 422, in __init__ for field_path, value in iterator: File "/layers/google.python.pip/pip/lib/python3.8/site-packages/google/cloud/firestore_v1/_helpers.py",
line 369, in extract_fields for s_path, s_value in extract_fields(value, field_path): File "/layers/google.python.pip/pip/lib/python3.8/site-packages/google/cloud/firestore_v1/_helpers.py",
line 369, in extract_fields for s_path, s_value in extract_fields(value, field_path): File "/layers/google.python.pip/pip/lib/python3.8/site-packages/google/cloud/firestore_v1/_helpers.py",
line 364, in extract_fields sub_key = FieldPath(key) File "/layers/google.python.pip/pip/lib/python3.8/site-packages/google/cloud/firestore_v1/field_path.py",
line 272, in __init__ raise ValueError(error) ValueError: One or more components is not a string or is empty.
When I run my code in a jupyter notebook, it all works fine. When I run it in cloud functions, I get the above error. When I copy the dictionary insides and hardcode it into the update command on cloud functions, it works. But when I print the dictionary to the cloud functions log, and then copy and paste that into the update function it does NOT work. Something seems to be happening with the cloud function that is messing up the dictionary.
This is the data on firestore (this is an old route currently in there)
