Firebase Cloud Functions会在24小时后而不是24小时后直接删除节点 [英] Firebase Cloud Functions deletes nodes directly after rather than 24 hours later

查看:66
本文介绍了Firebase Cloud Functions会在24小时后而不是24小时后直接删除节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是删除所有使用Firebase Cloud Functions和实时数据库发送的消息节点24小时.我尝试复制并粘贴这篇文章中的答案,但是由于某些原因邮件在创建后立即删除,而不是在24小时后删除.如果有人可以帮助我解决这个问题,我将不胜感激.我根据相同的问题尝试了多种不同的答案,但它们对我没有用.

My goal is to delete all the message nodes 24 hours after they were sent using Firebase Cloud Functions and the Realtime Database. I tried copy and pasting the answer from this post however for some reason the messages delete directly after they were created rather than the 24 hours later. If someone could help me solve this problem I would really appreciate it. I have tried multiple different answers based on the same issue and they haven't worked for me.

这是我的index.js文件:

Here is my index.js file:

'use strict';

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();

// Cut off time. Child nodes older than this will be deleted.
const CUT_OFF_TIME = 24 * 60 * 60 * 1000; // 2 Hours in milliseconds.


exports.deleteOldMessages = functions.database.ref('/Message/{chatRoomId}').onWrite(async (change) => {
const ref = change.after.ref.parent; // reference to the parent
const now = Date.now();
const cutoff = now - CUT_OFF_TIME;
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);
});

我的数据库结构是:

推荐答案

在注释中,您表示您正在使用Swift.从中和屏幕截图可以看出,您存储的时间标记是自1970年以来的秒数,而Cloud Functions中的代码假定该时间单位是毫秒.

In the comments you indicated that you're using Swift. From that and the screenshot it turns out that you're storing the timestamp in seconds since 1970, while the code in your Cloud Functions assumes it is in milliseconds.

最简单的解决方法是:

// Cut off time. Child nodes older than this will be deleted.
const CUT_OFF_TIME = 24 * 60 * 60 * 1000; // 2 Hours in milliseconds.


exports.deleteOldMessages = functions.database.ref('/Message/{chatRoomId}').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);
});

也请在此处查看我的答案:

Also see my answer here: How to remove a child node after a certain date is passed in Firebase cloud functions?

这篇关于Firebase Cloud Functions会在24小时后而不是24小时后直接删除节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆