【发布时间】: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