Python ValueError:尝试使用云函数中的字典更新Firestore文档时,一个或多个组件不是字符串或为空
我试图用我的python脚本创建一个Google Cloud函数,但我一直得到一个错误。尝试使用嵌套字典更新firestore文档中的字段时出错。下面是我的代码片段:Python ValueError:尝试使用云函数中的字典更新Firestore文档时,一个或多个组件不是字符串或为空,python,google-cloud-firestore,google-cloud-functions,Python,Google Cloud Firestore,Google Cloud Functions,我试图用我的python脚本创建一个Google Cloud函数,但我一直得到一个错误。尝试使用嵌套字典更新firestore文档中的字段时出错。下面是我的代码片段: driverDB = driverDB = db.collection('drivers') driverList = [] for doc in driverDB.get(): driverList.append(doc.id) for driver in range(0,len(driverList)): r
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)
我得到的错误是:
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.
RouteDataICT看起来像这样:
{'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)}}
driverList[driver]正确地打印出文档ID的字符串,因此错误似乎在字典中
完整错误堆栈:
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.
当我在jupyter笔记本上运行我的代码时,一切正常。当我在云函数中运行它时,我得到了上面的错误。当我将字典内部复制并硬编码到云函数的update命令中时,它就工作了。但是,当我将字典打印到云函数日志中,然后将其复制并粘贴到更新函数中时,它就不起作用了。云函数似乎发生了一些事情,把字典搞乱了
这是firestore上的数据(这是一条旧路线,目前在那里)
您确定
doc.id
不是整数吗?如果它是一个整数,那么请尝试将其转换为字符串(
doc.id.toString()
),因为firestore只接受key作为string
或null
错误发生前driverList[driver]
的值到底是多少?您好@DougStevenson这是一个字符串,是文档id的名称。它是用户的UID。我已经通过向update命令传递一个简单的字典来测试了这一点,更新工作并更新firestore中的文档。似乎是嵌套字典的问题。那么,当您记录它时,它的确切值是什么?当我记录UID时,它是一些字符串,如“Lu2qHfOqtma48r7L2g4PsEDQizD2”,它属于str类。该字典与上面发布的字典类似。有趣的是,当我将jupyter笔记本中的字典复制并粘贴到云函数中时,它就起作用了。但是,当我将字典打印到我的谷歌云函数日志中,然后将其复制并粘贴到云函数的硬编码中时,它就不起作用了。谷歌云函数中出现了无法正确处理字典的情况。在jupyter笔记本中运行时,我的所有代码都运行得非常好。您能否像firestore上一样共享数据结构。stop1
是地图吗?是,地图中包含字符。这是firebase身份验证UID字符串。我还通过一个简单的测试字典对它进行了测试,并且它进行了正确的更新