使用 Cloud Functions 通过 Firestore 观看集合组

jon*_*nyc 5 firebase google-cloud-functions google-cloud-firestore

我正在开发一个使用 collectionGroups 的 Firestore DB。

所讨论的 collectionGroup 是“Fights”的集合。

当创建新的战斗时,我想使用云函数中的 onCreate 方法来监视新的“战斗”条目,然后向其中添加一些元数据。理想情况下它看起来像下面的伪代码

export const placeFightersInEvent = functions.firestore
  .collectionGroup('fights/{fightId}')
  .onCreate(async (fightSnapshot, context) => {
    // get metadata and add to the newly created 'fight'
  });
Run Code Online (Sandbox Code Playgroud)

我正在使用最新版本的 firebase 函数和 admin sdk,但我似乎找不到可用的函数来执行此操作。可以用这种方式观看收藏组吗?

Dou*_*son 8

目前,这对于任何深度的战斗子集合都是不可能的。请向 Firebase 支持提交功能请求如果您需要这样做,

但是,如果您只在已知深度上使用过战斗子集合,那么无论如何这也可能有效:

export const placeFightersInEvent = functions.firestore
  .document('{coll}/{doc1}/fights/{doc2}')
  .onCreate(async (fightSnapshot, context) => {
    // get metadata and add to the newly created 'fight'
  });
Run Code Online (Sandbox Code Playgroud)

它应该只触发任何顶级集合中嵌套在文档下方的新战斗,因此它不是真正的集合组事件处理程序。但是您可以为所需的每个深度创建新函数。

  • 只是为了将来向任何人澄清,使用 context.params.eventID 访问 eventID 并使用 context.params.fightId 访问 FightId。这很完美。再次感谢。 (4认同)