根据Firestore更改触发云功能

时间:2019-08-29 13:51:08

标签: python firebase google-cloud-firestore

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

所有这些都在python中。

我尝试过这样:

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

城市->消防站集合。

这是我的回调函数-

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

此query_watch属于Watch对象类型。如何从中提取细节,例如什么是增量??

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

1 个答案:

答案 0 :(得分: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()将运行两次,因为:

  

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