【问题标题】:Delete item after a date (Firebase Cloud function)在某个日期之后删除项目(Firebase Cloud 功能)
【发布时间】: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-functionsfirebase-admin 软件包的哪个版本?云端功能日志是否有错误?
  • 不,我没有任何错误,我的函数正在运行。
  • 你好孟菲斯,你能解决这个问题吗?我现在面临同样的问题
  • 抱歉延迟@olajide,我没有收到来自 Stackoverflow 的通知...我提出的解决方案是使用 Cron Job 来管理我的记录的删除。 cron-job.org/en

标签: javascript node.js firebase google-cloud-functions


【解决方案1】:

代码没有问题,只需更新您的云功能和管理:

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
exports.deleteOldItems = functions.database.ref("/events/{eventId}").onWrite((change, context) => {
  if (change.after.exists() && !change.before.exists()) {
    const ref = change.after.ref.parent;
    const now = Date.now();
    const oldItemsQuery = ref.orderByChild('MyTimestamp').endAt(now);
    return oldItemsQuery.once('value').then((snapshot) => {
      const updates = {};
      snapshot.forEach(child => {
        updates[child.key] = null;
      });
      return ref.update(updates);
    });
  } else {
    return null;
  }
});

在函数文件夹中运行以下命令:

npm install firebase-functions@latest --save npm install firebase-admin@5.11.0 --save

更多详情请参考here

【讨论】:

  • 谢谢你的回答,我试过了,现在功能被触发了,但一直都是!无论Date.now()timestamps 比较...每条新记录都会被删除。
  • 你希望函数什么时候触发?
  • 我已经更新了仅在添加新事件时才执行的函数。请测试并告诉我它是否按预期工作
  • 非常感谢您的帮助!我希望当事件(他的timestamps)比Date.now() 日期更早时触发该功能。我希望这个函数像看门狗一样,当一个事件结束时,它会被自动删除。
  • 基本上,当item 1MyTimestamp: 1497911193083Date.now()老时,item 1被删除。
【解决方案2】:

尝试改变

 admin.initializeApp();

到:

admin.initializeApp(functions.config().firebase);

【讨论】:

  • 确实,要触发函数,用admin.initializeApp(functions.config().firebase)效果更好,但我现在有一个新问题,函数正在删除所有新记录...
  • @Memphis 因为间隔是“现在”.endAt(now)。再次检查 Firebase 示例。
猜你喜欢
  • 1970-01-01
  • 2019-03-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-12
相关资源
最近更新 更多