我一直在很好地使用 firestore 事務,并一直在嘗試實現一些 RTDB 版本。我有一棵帶有自動生成鍵的樹。這些鍵的值是映射,其中一個鍵是“uid”,例如"AUTOGENKEY" : { "uid" : 'a uid'}, ...etc我想要一個可以刪除所有單身用戶節點的事務...如果用戶在事務期間創建了任何新節點,它應該重試并將新節點包含在事務刪除中。我目前有這個await rtdb.ref(‘someRef’) .orderByChild(‘uid’) .equalTo(uid) .once('value') .transaction(function(currentVal) { // Loop each of the nodes with a matching ‘uid’ and delete them // If any of the nodes are updated (or additional nodes are created with matching uids) // while the transaction is running it should restart and retry the delete // If no nodes are matched nothing should happen });但是我想仔細檢查我是否需要在 currentVal 回調中進行另一個事務,以及我是否可以只返回 null 來刪除每個節點。我一直在使用這個答案作為參考Firebase 數據庫事務搜索和更新親切的問候- 編輯新方法坦率地說,我聽取了您的建議,最終只是像這樣存儲我的數據:uid -> counter 我不知道交易不能在查詢中運行,謝謝你讓我知道。我需要能夠從 uid 計數中添加/減去數量,如果它導致數字低于 0,則應刪除該節點。如果我將 null 作為數量傳遞,它應該刪除該節點。這就是我目前擁有的。async function incrementOrDecrementByAmount(pathToUid, shouldAdd, amount, rtdb){ await rtdb.ref(pathToUid) .transaction(function(currentVal) { if(currentVal == null || amount == null) { return amount; }else{ let newAmount = null; // Just sum the new amount if(shouldAdd == true) { newAmount = currentVal + amount; } else { const diff = currentVal - amount; // If its not above 0 then leave it null so it is deleted if(newAmount > 0) { newAmount = diff; } } return newAmount; } });}如果我有以下執行,我不確定第一個 if 語句。incrementOrDecrementByAmount (somePath, 10, true, rtdb)incrementOrDecrementByAmount (somePath, 100, false, rtdb)這總是會導致節點被刪除嗎?交易是否始終取決于調用順序,或者它是關于誰先完成的競爭條件。
firebase RTDB 事務刪除
慕萊塢森
2023-06-15 10:22:39