【问题标题】:How to correct Firebase Cloud Function admin reference not a function error如何更正 Firebase Cloud Function 管理员参考而不是函数错误
【发布时间】: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


【解决方案1】:

错误信息告诉你,这行代码正在调用一个不存在的方法:

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

事实上,admin.database() 返回的 Database 对象上并没有名为“reference”的方法。使用 API 文档,您可以看到 database() 返回一个 Database 对象,该对象进一步扩展了一个不同的 Database。在那里,您会看到有一个ref() 方法。没有reference()。也许这就是你想要使用的。

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

此外,这也是您将在documentation 的示例代码中看到的内容。请检查以确保您遵循正确的 JavaScript 示例。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-13
    • 2020-07-06
    • 2019-07-18
    • 1970-01-01
    相关资源
    最近更新 更多