我使用的是我前一段时间在这里找到的函数。问题是我的Firebase表具有动态名称,因此我无法深入查看每个项目。
数据为:
Image: Note that my timestamp is inside each entry (post-NNNN).
下面是代码。它可以工作,但是只会删除我创建新数据的post / Postid内部包含的旧数据。
问题:我一直在寻找一种方法来进行“ foreach”操作,并在每个nnnn内查看以删除较旧的数据。
exports.deleteOldItems = functions.database.ref("postslive/{userId}/{postId}")
.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);
});
});