【发布时间】:2018-12-10 14:50:54
【问题描述】:
我正在尝试编写一个 firebase 云函数,以便在日期过后自动删除事件。
基于这个例子Firebase example,我来到了这个,但是当我在 Firebase 上上传它时,它在 Firebase 端运行,但它没有删除事件。
你们有什么建议或发现我的代码有问题吗?问题是否可能来自触发器onWrite()?
/* My database structure
/events
item1: {
MyTimestamp: 1497911193083
},
item2: {
MyTimestamp: 1597911193083
}
...
*/
// Cloud function to delete events after the date is passed
'use strict';
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
exports.deleteOldItems = functions.database.ref('/events/{eventId}').onWrite((change) => {
const ref = change.after.ref.parent; // reference to the parent
const now = Date.now();
const oldItemsQuery = ref.orderByChild('MyTimestamp').endAt(now);
return oldItemsQuery.once('value').then((snapshot) => {
// create a map with all children that need to be removed
const updates = {};
snapshot.forEach(child => {
updates[child.key] = null;
});
return ref.update(updates);
// execute all updates in one go and return the result to end the function
});
});
【问题讨论】:
-
您使用的是
firebase-functions和firebase-admin软件包的哪个版本?云端功能日志是否有错误? -
不,我没有任何错误,我的函数正在运行。
-
你好孟菲斯,你能解决这个问题吗?我现在面临同样的问题
-
抱歉延迟@olajide,我没有收到来自 Stackoverflow 的通知...我提出的解决方案是使用 Cron Job 来管理我的记录的删除。 cron-job.org/en
标签: javascript node.js firebase google-cloud-functions