[英]Firestore error: A document must have an even number of path elements
I'm trying to update a document in Firestore and add a few fields that track the status of processing of that file.我正在尝试更新 Firestore 中的文档并添加一些字段来跟踪该文件的处理状态。
The problem is that I'm getting the error: "A document must have an even number of path elements"问题是我收到错误:“文档必须有偶数个路径元素”
However, the path of the document is:但是,文档的路径是:
orders/orders_files_logs/orders_processing_status/A2EUQ1WTGCTBG2_Orders-2022_08_18-01-03-24
I've have a method in Python that I wrote and call when updating a document and it works, for documents in a different collection.我在 Python 中有一个方法,我在更新文档时编写并调用了该方法,它适用于不同集合中的文档。 The path to the other collection is shorter, though:
/orders/orders_metadata
.但是,另一个集合的路径更短:
/orders/orders_metadata
。
I'm not exactly sure what I can do to fix this.我不确定我能做些什么来解决这个问题。 The definiton of the Python method is simply:
Python 方法的定义很简单:
def update_firestore_document(self, document_id, updated_information):
self._collection.document(document_id).update(updated_information)
And I call this method with:我用以下方法调用此方法:
update_firestore_document(
document_id = 'orders/orders_files_logs/orders_processing_status/A2EUQ1WTGCTBG2_Orders-2022_08_18-01-03-24',
updated_information = {'processed':'Success'})
Managed to found out the problem myself.设法自己发现了问题。 When initializing the connection to the Firestore database using:
使用以下命令初始化与 Firestore 数据库的连接时:
firestore_status_processor = FirestoreConnector(collection='orders', app_name='orders_processor')
where the definition of the FirestoreConnector
class is:其中
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)
It seems that in my original code, where I call the firestore_status_processor.update_firestore_document()
with the parameters I mentioned in my question, which the most important one is:似乎在我的原始代码中,我使用我在问题中提到的参数调用了
firestore_status_processor.update_firestore_document()
,其中最重要的是:
document_id = 'orders/orders_files_logs/orders_processing_status/A2EUQ1WTGCTBG2_Orders-2022_08_18-01-03-24'
By manually adding in the orders/
at the beginning, I am essentially breaking the path that is defined by the default Firebase client.通过在开头手动添加
orders/
,我基本上破坏了默认 Firebase 客户端定义的路径。
When debugging I notice that the actual path it was trying to write to was:调试时,我注意到它试图写入的实际路径是:
projects/(project-id)/database/(default)/documents/orders//orders/order_files_logs/orders_processing_status/A2EUQ1WTGCTBG2_Orders-2022_08_18-01-03-24
Removing the extra orders/
from the document_id parameter, when calling the update_firestore_document method did the trick.在调用 update_firestore_document 方法时,从 document_id 参数中删除额外的
orders/
就可以了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.