【发布时间】:2018-02-24 15:16:18
【问题描述】:
我正在我的应用中尝试 Firebase Cloud Functions,使用此代码在创建 2 小时后删除数据。
exports.deleteOldItems = functions.database.ref('/Rooms/{pushId}')
.onWrite(event => {
var ref = event.data.ref.parent; // reference to the items
var now = Date.now();
var cutoff = now - 2 * 60 * 60 * 1000;
var oldItemsQuery = ref.orderByChild('timestampCreated/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);
});
});
这行得通。现在我想写另一个ref(例如:/Users/{userID}/)每次删除数据时。问候
【问题讨论】:
-
您在实现该要求时遇到了什么问题?
-
@FrankvanPuffelen 嗨弗兰克,问题是我不知道如何编写承诺,因为我看到的示例总是在上述相同的参考中进行修改(exports.deleteOldItems = functions.database .ref('/Rooms/{pushId}') 例如)。谢谢
标签: javascript firebase firebase-realtime-database google-cloud-functions