【发布时间】:2022-02-14 04:30:40
【问题描述】:
我最近尝试将 .WriteOn 云功能更新为我的 Firebase 应用程序的预定云功能。目标是每 4 天运行一个函数,删除超过 2 天的消息。这对于 .WriteOn 函数非常有效,但当然这意味着每次创建消息时都会执行该函数;这是矫枉过正。这是我在 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 96 hours').onRun(async (context) => {
const ref = admin.database().reference('messages/{pushId}');
var now = Date.now();
var cutoff = now - 24 * 60 * 60 * 1000;
var oldItemsQuery = ref.orderByChild('timeStamp').endAt(cutoff);
return oldItemsQuery.once('value', function(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 Functions 控制台上看到的执行错误...
scheduledFunction TypeError: admin.database(...).reference is not a 功能
【问题讨论】:
-
我认为您的意思不是 WriteOn,而是 onWrite。
-
我也删除了标签和所有对 Flutter 的引用,但这里根本不涉及。这完全是运行 nodejs 的 Cloud Functions 上的 JavaScript。根据客户端应用的语言或平台,这不会发生任何变化。
标签: javascript node.js firebase google-cloud-functions