【问题标题】:How to set up a realtime listener for document title within firebase collection - python如何在firebase集合中为文档标题设置实时监听器 - python
【发布时间】:2019-11-02 16:27:18
【问题描述】:

我正在尝试收听 Firebase 集合以创建文档。

firebase 文档是这样列出的:

# Create a callback on_snapshot function to capture changes
def on_snapshot(col_snapshot, changes, read_time):
    print(u'Callback received query snapshot.')
    print(u'Current cities in California:')
    for doc in col_snapshot:
        print(u'{}'.format(doc.id))

col_query = db.collection(u'cities').where(u'state', u'==', u'CA')

# Watch the collection query
query_watch = col_query.on_snapshot(on_snapshot)

有没有什么方法可以引用文档标题,而不是 where() 中的 u'state'? (文档中没有标题)

我尝试过使用u'__name__',但出现错误:

RuntimeError: Error 3: a filter on __name__ must be a document resource name

【问题讨论】:

    标签: python firebase google-cloud-firestore


    【解决方案1】:

    JavaScript SDK 似乎有一个明确的选项,如 here 所示:

    db.collection('books').where(firebase.firestore.FieldPath.documentId(), '==', 'fK3ddutEpD2qQqRMXNW5').get()
    

    所以firebase.firestore.FieldPath.documentId()表示文档ID。

    Python 中没有直接的等价物,但您可以通过查询 __name__(该字段的内部名称)来完成相同的操作:

    col_query = db.collection(u'cities').where(u'__name__', u'==', u'your_document_id')
    

    【讨论】:

    • 查询__name__返回以下错误:RuntimeError: Error 3: a filter on __name__ must be a document resource name
    • 我能想到一些变通办法,但这似乎应该是可能的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-27
    • 2021-01-23
    • 2012-06-06
    • 1970-01-01
    • 1970-01-01
    • 2021-10-05
    • 2018-10-03
    相关资源
    最近更新 更多