婷婷同學_
2019-07-05 15:25:41
刪除2小時以上的火災(zāi)數(shù)據(jù)我想刪除兩個小時以上的任何數(shù)據(jù)。目前,在客戶端,我循環(huán)遍歷所有數(shù)據(jù),并在任何較舊的數(shù)據(jù)上運行DELETE。當我這樣做時,每次刪除某個內(nèi)容時都會調(diào)用db.on(‘value’)函數(shù)。另外,只有在客戶端連接時才會刪除這些內(nèi)容,如果兩個客戶端同時連接,會發(fā)生什么情況?我可以在哪里設(shè)置刪除舊數(shù)據(jù)的東西?我在JavaScriptDate.Now()創(chuàng)建的每個對象中都有一個時間戳。
3 回答

海綿寶寶撒
TA貢獻1809條經(jīng)驗 獲得超8個贊
var ref = firebase.database().ref('/path/to/items/');var now = Date.now();var cutoff = now - 2 * 60 * 60 * 1000; var old = ref.orderByChild('timestamp').endAt(cutoff).limitToLast(1);var listener = old.on('child_added', function(snapshot) { snapshot.ref.remove();});
child_added
value
limitToLast(1)
child_added
更新
exports.deleteOldItems = functions.database.ref('/path/to/items/{pushId}').onWrite((change, context) => { var ref = change.after.ref.parent; // reference to the items var now = Date.now(); var cutoff = now - 2 * 60 * 60 * 1000; var oldItemsQuery = ref.orderByChild('timestamp').endAt(cutoff); return oldItemsQuery.once('value', function(snapshot) { // create a map with all children that need to be removed var updates = {}; snapshot.forEach(function(child) { updates[child.key] = null }); // execute all updates in one go and return the result to end the function return ref.update(updates); });});
/path/to/items
functions-samples

茅侃侃
TA貢獻1842條經(jīng)驗 獲得超21個贊
添加回答
舉報
0/150
提交
取消