如何更正 Firebase Cloud Function admin reference not a function error
How to correct Firebase Cloud Function admin reference not a function error
我最近尝试将 .WriteOn 云函数更新为我的 firebase 应用程序的预定云函数。 objective 是 运行 一个函数,每 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
function
错误消息告诉您这行代码正在调用一个不存在的方法:
const ref = admin.database().reference('messages/{pushId}');
事实上,admin.database()
返回的数据库对象上没有名为“reference”的方法。使用 API 文档,您可以看到 database() returns a Database object which further extends a different Database。在那里,您会看到有一个 ref()
方法。没有reference()
。也许这就是你想要使用的。
const ref = admin.database().ref('messages/{pushId}');
此外,这就是您将在 documentation 中的示例代码中看到的内容。请检查以确保您遵循 JavaScript.
的正确示例
我最近尝试将 .WriteOn 云函数更新为我的 firebase 应用程序的预定云函数。 objective 是 运行 一个函数,每 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 function
错误消息告诉您这行代码正在调用一个不存在的方法:
const ref = admin.database().reference('messages/{pushId}');
事实上,admin.database()
返回的数据库对象上没有名为“reference”的方法。使用 API 文档,您可以看到 database() returns a Database object which further extends a different Database。在那里,您会看到有一个 ref()
方法。没有reference()
。也许这就是你想要使用的。
const ref = admin.database().ref('messages/{pushId}');
此外,这就是您将在 documentation 中的示例代码中看到的内容。请检查以确保您遵循 JavaScript.
的正确示例