【问题标题】:Firebase Cloud Functions deletes nodes directly after rather than 24 hours laterFirebase Cloud Functions 在 24 小时后直接删除节点
【发布时间】:2019-10-12 12:44:20
【问题描述】:

我的目标是在使用 Firebase Cloud Functions 和实时数据库发送消息节点后 24 小时删除所有消息节点。我尝试从this post 复制并粘贴答案,但由于某种原因,消息在创建后直接删除,而不是在 24 小时后删除。如果有人可以帮助我解决这个问题,我将不胜感激。我已经根据同一个问题尝试了多个不同的答案,但它们对我没有用。

这是我的 index.js 文件:

'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);
});

而我的数据库结构是:

【问题讨论】:

  • 许多开发人员报告了实现此功能的问题,但就像here last week 一样,问题始终在于他们所做的更改。例如:登录时cutoff的值是多少?这与 JSON 中 seconds 的值相比如何?
  • @FrankvanPuffelen 很抱歉提出这个问题,但您记录它是什么意思以及如何记录?存储的秒数来自 1970 年以来的秒数
  • console.log(cutoff) 然后检查您的 Cloud Functions 的日志记录面板。如果您是 JavaScript 新手,Cloud Functions for Firebase 并不是学习它的最佳方式。我建议先阅读Firebase documentation for Web developers。这涵盖了许多基本的 JavaScript 和 Firebase 数据库交互。您还可以在本地 Node.js 进程中使用 Admin SDK,该进程可以使用本地调试器进行调试。完成这些之后,您将能够更好地为 Cloud Functions 编写代码。
  • @FrankvanPuffelen 对于迟到的回复,我深表歉意。我记录了它并在控制台中得到了 1559331549417 。接下来我该怎么办?感谢您对此提供的帮助
  • @DheerajMahra 是的,但您仍然可以获得使用免费限制的 spark 计划。您还可以设置付款限额。

标签: node.js firebase firebase-realtime-database google-cloud-functions


【解决方案1】:

在 cmets 中,您表示您正在使用 Swift。从那和屏幕截图可以看出,自 1970 年以来,您以秒为单位存储时间戳,而 Cloud Functions 中的代码假定它以毫秒为单位。

最简单的解决方法是:

// 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);
});

也可以在这里查看我的答案:How to remove a child node after a certain date is passed in Firebase cloud functions?

【讨论】:

  • 我尝试这样做,但我仍然得到与以前相同的响应,我不明白。我还查看了其他答案,但该功能是在 HTTP 而不是 onWrite 中完成的
  • 函数的触发方式应该不会有太大的不同,只要您连接ref。如果它仍然不起作用,请执行与之前相同的操作:记录 cutoff 并将其与数据库中的值进行比较。
  • 我尝试了所有这些,但由于某种原因,我仍然得到相同的结果。我还尝试将数据库中的秒数设为毫秒并使用原始函数,它们仍然会立即删除。您对我如何做到这一点有什么建议吗?它就在工作的边缘,我觉得它只需要稍微改变一下。日志中的值还是和以前一样。
  • 原始代码中的问题是我回答的秒与毫秒比较。如果您记录cutoff 的值,您可以看到您将数据库中的seconds 值与什么进行比较。如果您告诉我们它记录了什么,我们可以提供帮助。
  • 数据库中存储的秒数是1560021980,而cutoff的日志是1560021905247。非常感谢您对我的帮助,我真的很感激!
猜你喜欢
  • 2019-10-01
  • 2018-04-12
  • 1970-01-01
  • 1970-01-01
  • 2019-03-14
  • 2017-10-17
  • 2019-12-18
  • 2018-11-04
  • 2017-09-11
相关资源
最近更新 更多