【问题标题】:Firebase scheduled cloud function not deleting nodesFirebase计划的云功能不删除节点
【发布时间】:2022-02-17 23:56:43
【问题描述】:

我的 Firebase 计划功能遇到了几个问题。我正在查看日志和文档,它正在运行并且记录为“成功”并且正常,但是我的实时数据库的所需节点没有更新/删除。这是我的 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" 添加到您的 安全规则以获得更好的性能。

因为这是一个警告,并且当它是 .onWrite 时该函数运行良好,所以我不确定是不是这样。

【问题讨论】:

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


    【解决方案1】:

    问题出在这里:

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

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

    您需要查询和更新:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-28
      • 2018-04-14
      • 2021-01-06
      • 2018-06-25
      • 2018-05-18
      • 1970-01-01
      • 2020-10-10
      • 2021-12-08
      相关资源
      最近更新 更多