【问题标题】:Fetching from firestore and Firebase functions timeout从 Firestore 和 Firebase 函数获取超时
【发布时间】:2022-02-09 02:34:13
【问题描述】:

我有一个简单的功能,我正在模拟器上进行本地测试。

    exports.sendNotification = functions.https.onRequest(() => {
  firebase
    .firestore()
    .collection("Users")
    .get()
    .then((snapshot) => {
      snapshot.docs.forEach((doc) => {
        console.log(doc.id, doc.data());
      });
    });
});

当我执行该函数时,它工作正常,但在我收到此消息后不久:

     functions: Your function timed out after ~60s. To configure this timeout, see
      https://firebase.google.com/docs/functions/manage-functions#set_timeout_and_memory_allocation.
>  C:\Users\myusername\AppData\Roaming\npm\node_modules\firebase-tools\lib\emulator\functionsEmulatorRuntime.js:618
>              throw new Error("Function timed out.");
>              ^
>
>  Error: Function timed out.
>      at Timeout._onTimeout (C:\Users\myusername\AppData\Roaming\npm\node_modules\firebase-tools\lib\emulator\functionsEmulatorRuntime.js:618:19)
>      at listOnTimeout (node:internal/timers:557:17)
>      at processTimers (node:internal/timers:500:7)

我的期望是函数会正常结束,因为它在函数的末尾,这条消息是说我需要做一些事情来表明函数已经完成吗?我是否需要更改某些内容,尤其是在投入生产时?

【问题讨论】:

    标签: node.js firebase google-cloud-firestore google-cloud-functions


    【解决方案1】:

    您必须通过发回响应来terminate a HTTP function,否则它将一直运行直到超时。尝试像这样重构您的代码:

    exports.sendNotification = functions.https.onRequest((req, res) => {
      // add req, res in functions parameters             ^^^
      return firebase
        .firestore()
        .collection("Users")
        .get()
        .then((snapshot) => {
          snapshot.docs.forEach((doc) => {
            console.log(doc.id, doc.data());
          });
          // Return response
          return res.json({ data: snapshot.docs.map(d => d.data()) })
        });
    });
    

    【讨论】:

    • 所以我要做的就是在一天中的随机时间从 firebase 执行这个函数。在所有触发器中,看起来 https.onRequest 是最合适的。我不需要函数本身在完成后返回任何内容。我什至尝试过退货;最后仍然得到超时错误。
    • @Reza 你仍然需要终止它。可以加res.json({})吗?只是一个要返回的空对象
    • 那行得通,只是做一个返回或返回“Ok”没有。
    • 这是一个 HTTP 函数,因此您需要返回/结束响应,这与后台函数不同,后台函数可以返回承诺或仅在同步函数的情况下返回。
    • 如果我只想在随机时间触发一些动作,这是最好的功能吗?我还没有完全弄清楚如何触发它。 Firebase 没有随机定时功能的选项 - 或者有吗?
    猜你喜欢
    • 2018-11-06
    • 1970-01-01
    • 2020-08-05
    • 2020-02-11
    • 2020-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多