exports.deleteOldMessages = functions.database.ref('[path]').onWrite(async (change) => {
const ref = change.after.ref.parent; // reference to the parent
const now = Date.now();
const cutoff = (now - CUT_OFF_TIME) / 1000; // convert to seconds
const oldItemsQuery = ref.orderByChild('seconds').endAt(cutoff);
const snapshot = await oldItemsQuery.once('value');
// create a map with all children that need to be removed
const updates = {};
snapshot.forEach(child => {
updates[child.key] = null;
});
// execute all updates in one go and return the result to end the function
return ref.update(updates);
});
1条答案
按热度按时间n3schb8v1#
您可以创建类似下面的方法,您需要将其上传到Firebase Cloud Function上
第一个参数为:// 2小时(以毫秒为单位)。
您也可以查看下面的链接以供参考。
souce