【问题标题】:How to perform NumChildren() inside a scheduled task in Firebase?如何在 Firebase 的计划任务中执行 NumChildren()?
【发布时间】:2019-10-29 14:47:14
【问题描述】:

我正在尝试每 5 分钟计算一次孩子的数量。 我一直在尝试编写一个每 X 分钟运行一次的代码,每次运行时,它都会计算孩子的数量。

我已经将 Firebase 文档中的代码用于计划函数和快照,但我可能遗漏了一些东西。

exports.scheduledFunction = functions.pubsub.schedule('every 5 
minutes').onRun((context) => {
  var ref = firebase.database().ref("location/");
  ref.once("value")
   .then(function(snapshot) {
     var a = snapshot.numChildren(); // 1 ("name")
    });
});

我在尝试部署到 Firebase 云时遇到控制台错误:

8:3 错误预期 catch() 或返回 promise/catch-or-return

9:11 警告 Unexpected function expression prefer-arrow-callback

9:11 错误每个 then() 都应该返回一个值或抛出 promise/always-return

【问题讨论】:

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


    【解决方案1】:

    由于 once() 调用从 Firebase 异步加载数据,因此您需要在代码完成时通知 Cloud Functions。为此,您可以从顶层代码返回一个承诺,然后从那里返回一个值。

    所以是这样的:

    exports.scheduledFunction = functions.pubsub.schedule('every 5 
    minutes').onRun((context) => {
      var ref = firebase.database().ref("location/");
      return ref.once("value")
       .then(function(snapshot) {
         var a = snapshot.numChildren(); // 1 ("name")
         return true;
        });
    });
    

    另见:

    【讨论】:

      猜你喜欢
      • 2016-05-24
      • 1970-01-01
      • 2012-02-07
      • 1970-01-01
      • 2021-08-20
      • 2019-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多