【发布时间】:2021-04-12 13:50:28
【问题描述】:
我需要定期从 Firebase 中删除旧数据。我找到了this solution,但未能成功。
终端告诉我functions[deleteOldItems(us-central1)]: Successful update operation.但是数据没有被删除。
这就是我所做的:
const functions = require('firebase-functions');
exports.deleteOldItems = functions.database.ref('/nodeThatContainsDataToBeDeleted')
.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);
});
});
timestamp 是 639915248.124176(来自 Swift)
数据库结构:
root : {
"nodeThatContainsDataToBeDeleted" : {
"-r5tyfX9FGC0glhgf78Ia" : {
"company" : "CompanyCo",
"name" : "Sam",
"phone" : "1212",
"imageURL" : "https://firebasestorage.googleapis.com/imageurl",
"timestamp" : 6.39915248124176E8
}
},
}
【问题讨论】:
-
该终端消息只是说明名为
deleteOldItems的云功能已成功部署。它与您的代码无关。 -
请在您的问题中包含您的数据库结构。
-
@samthecodingman 包括数据库结构。
nodeThatContainsDataToBeDeleted直接在根目录下 -
您所指的SO问题说:“只要在/path/to/items下写入数据,此函数就会触发,因此只有在修改数据时才会删除子节点。”你是如何触发这个功能的?
标签: node.js firebase firebase-realtime-database google-cloud-functions