簡體   English   中英

Firebase 功能:監聽所有刪除更改

[英]Firebase functions: Listen to all delete changes

我有一個這樣的數據庫:

- users
    - userId1
        - activities
            - football: "20€"
        - competitions
            - competitionId1
                - football
                    - prize: "40€"

    - userId2
        - activities
            - baskets: "20€"
            - football: "30€"
        - competitions
            - competitionId4
                - basket
                    - prize: "40€"

所以我想收到所有刪除的通知,我嘗試使用

functions.database.ref("users/{userId}").onDelete();

但它只聽users/{userId} ,我可以使用.onUpdate() ,但我真的需要知道刪除的 object 的整個路徑。

我唯一能做的就是使用.onUpdate()並在舊值和新值之間進行深入比較,以檢索刪除了哪些元素。

但我認為這不是最好的解決方案

例如:

如果我刪除users/userId1/competitions

我將收到刪除users/userId1/competitions的通知

查看文檔中的此部分,了解由實時數據庫事件觸發的 Cloud Functions。

路徑規范匹配所有觸及路徑的寫入,包括發生在其下方任何位置的寫入。

...

如果事件數據可能很大,請考慮在更深的路徑上使用多個函數,而不是在數據庫根附近使用單個 function 為獲得最佳性能,請僅在可能的最深級別請求數據。

是否應該定義多個 Cloud Functions 完全由您決定。 但是,由於您想收聽刪除,因此為每個級別編寫 Cloud Function 可能會更容易,例如:

functions.database.ref("users/{userId}").onDelete(...);

functions.database.ref("users/{userId}/{activitiesOrCompetitions}").onDelete(...);

functions.database.ref("users/{userId}/{activitiesOrCompetitions}/{CompetitionId}").onDelete(...);

請注意,每次您刪除CompetitionId節點時,如果該用戶沒有更多的competitions ,您將觸發上面的第二個 Cloud Function。

同樣,您可以決定是否要觸發多個 Cloud Functions 進行一次刪除,或者您更願意僅觸發一個 Cloud Function(使用onUpdate() )並比較快照,正如您在問題中提到的那樣。 請記住,對於每月 200 萬次的 Cloud Functions 執行,有一個慷慨的免費套餐。

您是否嘗試將偵聽器更改為:

functions.database.ref("users/{userId}/{path}").onDelete();

然后在您的context中的path將是“活動”、“比賽”等。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM