使用 Firebase 的 Cloud Firestore 在 Python 3.7 中编写一个非常简单的程序。在程序中,我使用 .where(( 下拉集合的一部分,然后使用 for 循环遍历它。当任何有空格的字段名被传递到 .where(( 中时,它会在命中循环时抛出错误 (google.api_core.exceptions.InvalidArgument: 400 Invalid property path "Insert field name here"
(。
我认为这应该有效,考虑到我可以使用这些字段名称来放置数据和下拉数据,只是不要对它们使用 .where((。我做错了什么?只是不支持空格吗?
这是我正在使用的代码的精简版本:
import firebase_admin
from firebase_admin import credentials, firestore
cred = credentials.Certificate(cert path)
applet = firebase_admin.initialize_app(cred)
db = firestore.client()
main_collection = db.collection(u'Referral_DB')
main_collection_obj = main_collection.where(u'English Name', u'==', u'Jerry').get()
for ref in main_collection_obj:
pass
在文档的限制部分找到空格需要通过反引号转义。
这有效:
main_collection_obj = main_collection.where(u'`English Name`', u'==', u'Jerry').get()
根据我们的工程师的说法,您应该使用field_path
来转义 ASCII 字母、数字和下划线以外的任何内容。
所以你的电话可能看起来像
main_collection.where(Client.field_path(u'English Name'), u'==', u'Jerry')