Firebase Cloud Functions会在24小时后而不是24小时后直接删除节点

编程入门 行业动态 更新时间:2024-10-25 02:27:49
本文介绍了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); });

也请在此处查看我的答案:如何在Firebase云函数中经过特定日期后删除子节点?

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小时后直接删除节点

本文发布于:2023-11-27 01:51:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1636080.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:小时后   会在   节点   而不是   Cloud

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!