【问题标题】:Firestore Cloud Function listen to subcollection but not parent collectionFirestore Cloud Function 监听子集合但不监听父集合
【发布时间】:2021-03-06 08:33:03
【问题描述】:

这是从 Firebase 的文档中复制的:- https://firebase.google.com/docs/firestore/extend-with-functions

评论说监听器将监听“用户”和所有子集合中的所有文档。

我怎样才能只收听子集合的所有文档而不收听父集合? (即,在此示例中,请勿收听“用户”中的文档。)

// Listen for changes in all documents in the 'users' collection and all subcollections
exports.useMultipleWildcards = functions.firestore
    .document('users/{userId}/{messageCollectionId}/{messageId}')
    .onWrite((change, context) => {
      // If we set `/users/marie/incoming_messages/134` to {body: "Hello"} then
      // context.params.userId == "marie";
      // context.params.messageCollectionId == "incoming_messages";
      // context.params.messageId == "134";
      // ... and ...
      // change.after.data() == {body: "Hello"}
    });index.js

【问题讨论】:

    标签: google-cloud-firestore google-cloud-functions


    【解决方案1】:

    根据文档Specify a group of documents using wildcards

    如果要将触发器附加到一组文档,例如某个集合中的任何文档,请使用 {wildcard} 代替文档 ID

    另外,我在这里搜索了一个similar question

    对文档下组织的子集合的更改不算作对文档本身的更改。它们不会在该文档上触发快照事件。

    收听子集合与收听顶级集合没有什么不同。在这两种情况下,监听器只会被该集合或子集合中的文档更改触发。

    那么我可以断定这是不可能的。

    【讨论】:

      猜你喜欢
      • 2023-04-10
      • 2021-03-10
      • 2020-05-18
      • 2021-10-05
      • 2020-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多