首页 > 解决方案 > Firestore文档(重新)创建未触发Firebase“onCreate”功能

问题描述

我有一个由 Firestore 文档创建(onCreate() 触发)触发的 Firebase 函数。

在相同的情况下,我需要在同一个文档上再次运行相同的函数。由于该函数很长,而且我不想复制它,所以我选择以 hacky 方式触发它:

  1. 将文档复制到临时集合中
  2. 删除原始文件
  3. 再次复制具有相同DocumentId的文档,并将副本中的数据放在其原始位置
  4. 删除副本

整个事情是通过交易完成的——我想一切都是在后台完成的。

由于步骤 3 使用 SET ( Transaction.set() ) 方法。为什么不触发 onCreate() 事件?

任何帮助或替代我的情况将不胜感激。

谢谢

标签: firebasegoogle-cloud-firestoregoogle-cloud-functionsgoogle-cloud-datastore

解决方案


Solved it on my own.

For some reason, doing one transaction that deletes the document, then creates it again, the onCreate() event is not triggered.

I ended up splitting the transaction into two parts. Transaction #1 copies the document in a temporary collection then delete the original. Transaction #2 reads the copy, writes the original document in its original collection, with its original DocumentId, and then deletes the temporary copy.

This way, there are a few milliseconds between the delete and create operation of a document, and they are not bundled in the same transaction.

This solved it. Even though I still don't get why the onCreate() event won't trigger by a Document Creation operation that is inside one Transaction.


推荐阅读