繁体   English   中英

Firebase预定云function不删除节点

[英]Firebase scheduled cloud function not deleting nodes

我的 Firebase 计划 function 遇到了几个问题。我正在查看日志和文档,它正在运行并且记录为“成功”并且正常,但是我的实时数据库的所需节点没有更新/删除。 这是我的 index.js 代码...

// The Cloud Functions for Firebase SDK to create Cloud Functions and setup triggers.
const functions = require('firebase-functions');

// The Firebase Admin SDK to access the Firebase Realtime Database.
const admin = require('firebase-admin');
admin.initializeApp();

exports.scheduledFunction = functions.pubsub.schedule('every 1 minutes').onRun((context) => {
  const ref = admin.database().ref('messages/{pushId}');
  var now = Date.now();
  var cutoff = now - 24 * 60 * 60 * 1000;
  var oldItemsQuery = ref.orderByChild('timestamp').endAt(cutoff);
          return oldItemsQuery.once('value')
              .then(snapshot => {
                  // create a map with all children that need to be removed
                  var updates = {};
                  snapshot.forEach(function (child) {
                      updates[child.key] = null
                  });
                  // execute all updates in one go and return the result to end the function
                  return ref.update(updates);
              });
});

以下是我在数据库引用中构建要在“截止”时删除的数据的方式...

在此处输入图像描述

我已经将我的 firebase 功能更新到最新版本。 可能导致此问题的唯一其他问题是我应该警告

考虑将 /messages/{pushId} 处的 ".indexOn": "timeStamp" 添加到您的安全规则中以获得更好的性能。

因为这是一个警告,而 function 在 was.onWrite 时工作正常,我不确定是不是这样。

问题在这里:

const ref = admin.database().ref('messages/{pushId}');

那里的{pushId}没有意义,并且导致您查询错误的节点。

您需要查询和更新:

const ref = admin.database().ref('messages');

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM