![](/img/trans.png)
[英]Firestore addDoc error:Invalid document reference. Document references must have an even number of segments, but
[英]Firestore error: A document must have an even number of path elements
我正在尝试更新 Firestore 中的文档并添加一些字段来跟踪该文件的处理状态。
问题是我收到错误:“文档必须有偶数个路径元素”
但是,文档的路径是:
orders/orders_files_logs/orders_processing_status/A2EUQ1WTGCTBG2_Orders-2022_08_18-01-03-24
我在 Python 中有一个方法,我在更新文档时编写并调用了该方法,它适用于不同集合中的文档。 但是,另一个集合的路径更短: /orders/orders_metadata
。
我不确定我能做些什么来解决这个问题。 Python 方法的定义很简单:
def update_firestore_document(self, document_id, updated_information):
self._collection.document(document_id).update(updated_information)
我用以下方法调用此方法:
update_firestore_document(
document_id = 'orders/orders_files_logs/orders_processing_status/A2EUQ1WTGCTBG2_Orders-2022_08_18-01-03-24',
updated_information = {'processed':'Success'})
设法自己发现了问题。 使用以下命令初始化与 Firestore 数据库的连接时:
firestore_status_processor = FirestoreConnector(collection='orders', app_name='orders_processor')
其中FirestoreConnector
class 的定义是:
def __init__(self, collection, app_name='[DEFAULT]'):
firebase_admin.initialize_app(self._credentials, {'projectId': self._project_id}, name=app_name)
self.__initialize_firestore_client_connection()
self.__set_firestore_collection(collection=collection)
def __initialize_firestore_client_connection(self):
self._database = firestore.client()
def __set_firestore_collection(self, collection):
self._collection = self._database.collection(collection)
似乎在我的原始代码中,我使用我在问题中提到的参数调用了firestore_status_processor.update_firestore_document()
,其中最重要的是:
document_id = 'orders/orders_files_logs/orders_processing_status/A2EUQ1WTGCTBG2_Orders-2022_08_18-01-03-24'
通过在开头手动添加orders/
,我基本上破坏了默认 Firebase 客户端定义的路径。
调试时,我注意到它试图写入的实际路径是:
projects/(project-id)/database/(default)/documents/orders//orders/order_files_logs/orders_processing_status/A2EUQ1WTGCTBG2_Orders-2022_08_18-01-03-24
在调用 update_firestore_document 方法时,从 document_id 参数中删除额外的orders/
就可以了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.