【问题标题】:Trigger Cloud Function based on Firestore changes根据 Firestore 更改触发 Cloud Function
【发布时间】:2020-01-02 19:41:18
【问题描述】:

我想在特定俱乐部的文档发生更改(创建、修改、删除)时触发我的云功能。我的云功能应该捕获哪些数据被更改,并进一步将其发布到 pub/sub。

所有这些都在 python 中。

我试过这样:

query = db.collection('cities') query_watch = col_query.on_snapshot(callback)

城市 -> Firestore 集合。

这是我的回调函数 -

def callback(col_snapshot, changes, read_time):
print('callback.')

此 query_watch 属于 Watch 对象类型。如何从中提取详细信息,例如 delta 是什么?

关注了这个文档 - https://firebase.google.com/docs/firestore/query-data/listen

【问题讨论】:

    标签: python firebase google-cloud-firestore


    【解决方案1】:

    来自文档:

    # Create a callback on_snapshot function to capture changes
    def on_snapshot(doc_snapshot, changes, read_time):
        for doc in doc_snapshot:
            # Here you retrieve the data
            print(u'Received document snapshot: {}'.format(doc.id))
    
    doc_ref = db.collection(u'cities').document(u'SF')
    
    # Watch the document
    doc_watch = doc_ref.on_snapshot(on_snapshot)
    

    在您的def callback(col_snapshot, changes, read_time) 中,您需要根据需要处理收到的数据。

    print() 将运行两次,因为:

    使用您提供的回调的初始调用会创建一个文档 立即使用单个文档的当前内容进行快照。 然后,每次内容更改时,另一个调用会更新文档 快照。

    【讨论】:

    • 注明。但是对于打印执行两次。第一次使用最初存在的所有 doc.id,第二次添加新的 doc.id。我的问题是为什么它被调用了两次?以及我们如何访问“更改”
    • 这是它的行为,它第一次给出当前内容的快照,并且只会在进一步更改时触发。阅读:firebase.google.com/docs/firestore/query-data/…
    猜你喜欢
    • 2020-02-04
    • 1970-01-01
    • 1970-01-01
    • 2020-10-26
    • 2022-06-15
    • 1970-01-01
    • 2021-08-03
    • 1970-01-01
    • 2018-03-27
    相关资源
    最近更新 更多