简体   繁体   English

Firestore 错误:文档必须具有偶数个路径元素

[英]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.

相关问题 Firestore addDoc 错误:文档参考无效。 文档引用必须有偶数个段,但 - Firestore addDoc error:Invalid document reference. Document references must have an even number of segments, but 文档引用在集合引用上必须有偶数段错误 - Document references must have an even number of segments error on a collection reference “FIRESTORE 内部断言失败:文档引用无效。文档引用必须有偶数个段,但 NewGame 有 1 个” - "FIRESTORE INTERNAL ASSERTION FAILED: Invalid document reference. Document references must have an even number of segments, but NewGame has 1" Flutter: PlatformException(error, Invalid document reference. Document references must have an even number of segments, but<x> 有 1,空)</x> - Flutter: PlatformException(error, Invalid document reference. Document references must have an even number of segments, but <x> has 1, null) 从 Firestore 文档中检索数字时出错 - Getting an error while retrieving a number from a Firestore document iOS Firestore '文档路径不能为空' - iOS Firestore 'Document path cannot be empty' 从 Firestore 获取文档时的错误处理 - Error handling when getting document from Firestore 如何仅在文档 Firestore 中显示具有相同 uid 的用户 - How to only show user that have same uid in document firestore Firestore Swift:获取存储在文档中的嵌套地图数量 - Firestore Swift: Get number of nested maps stored in a document Firestore - 获取路径未知的 ID 文档 - Firebase Cloud Functions - Firestore - Get document with ID whose path is unknown - Firebase Cloud Functions
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM