Cloud Function会立即删除节点,而不是根据时间戳记删除两个小时后删除节点(删除旧子节点不起作用) [英] Cloud Function deletes node instantly instead of after two hours based off timestamp (delete-old-child-nodes not working)

查看:72
本文介绍了Cloud Function会立即删除节点,而不是根据时间戳记删除两个小时后删除节点(删除旧子节点不起作用)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现删除旧的子节点"云功能示例(

I am trying to implement the "delete-old-child-nodes" Cloud Functions example (https://github.com/firebase/functions-samples/tree/master/delete-old-child-nodes). When the directory is written to, it should delete all nodes that are 2+ hours old. However, it just deletes all nodes no matter what.

我发现此StackOverflow帖子描述相同的问题.但是,我相信原因是不同的.用户以秒为单位存储其时间戳,而截止时间以毫秒为单位.在我的代码中,两者都以毫秒为单位.

I found this StackOverflow post that describes the same issue. However, I believe the cause is different. The user was storing his timestamp in seconds, while the cutoff was in milliseconds. In my code, both are in milliseconds.

这是当前的index.js代码,它与Firebase示例完全相同,除了我将时间戳称为"lastUpdated"而不是"timestamp".

This is the current index.js code, it is exactly the same as the Firebase example, except for that I call the timestamp "lastUpdated," and not "timestamp."

'use strict';

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

const CUT_OFF_TIME = 2 * 60 * 60 * 1000; 

exports.deleteOldItems = functions.database.ref('/decks/{pushId}').onWrite(async (change) => {
  var ref = change.after.ref.parent; 
  const now = Date.now();
  const cutoff = now - CUT_OFF_TIME;
  const oldItemsQuery = ref.orderByChild('lastUpdated').endAt(cutoff);
  const snapshot = await oldItemsQuery.once('value');
  const updates = {};

  ref = ref.parent.child('deckLocs')

  snapshot.forEach(child => {
    updates[child.key] = null;
  });

  return ref.update(updates);
});

这是我的数据库结构

"xxxx-9d272" {
 "decks" : {
    "randomID12345" : {
      "lastUpdated" : 1558460300472
    },
    "randomID67890" : {
      "lastUpdated" : 1558460300472
    }
 },
 "deckLocs" : {
    "randomID12345" : {},
    "randomID67890" :{}
 }
}

谢谢.

推荐答案

问题是我试图将更新应用到另一个目录,而行ref = ref.parent.child('deckLocs')引起了问题.如果消失了,它将按预期工作.

The issue was that I was trying to apply the updates to another directory and the line ref = ref.parent.child('deckLocs') was causing issues. If it's gone it works as intended.

这篇关于Cloud Function会立即删除节点,而不是根据时间戳记删除两个小时后删除节点(删除旧子节点不起作用)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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