【问题标题】:Cloud Function writing to Firebase Realtime Database times out [duplicate]Cloud Function 写入 Firebase 实时数据库超时 [重复]
【发布时间】:2018-05-01 04:27:47
【问题描述】:

我可以看到在获得 URL 后立即添加了一个新条目,但请求挂起并超时。为什么?

const functions = require('firebase-functions');
const admin = require('firebase-admin');

admin.initializeApp(functions.config().firebase);

exports.add = functions.https.onRequest((request, response)=>
{
    var ref = admin.database().ref("jobs");
    var childRef = ref.push();
    childRef.set
    (
        {
            title: "test",
            pay: 100
        }
    );
})

代码基于以下示例。 https://firebase.google.com/docs/database/admin/save-data

结果

{"error":{"code":500,"status":"INTERNAL","message":"function execution attempt timed out"}}

【问题讨论】:

    标签: javascript firebase firebase-realtime-database google-cloud-functions


    【解决方案1】:

    由 HTTP 请求触发的 Cloud Functions 需要以send()redirect()end() 结束,否则它们将继续运行并达到超时。

    来自terminate HTTP functions section of the documentation on HTTP triggers

    始终以 send()redirect()end() 结束 HTTP 函数。否则,您的函数可能会继续运行并被系统强制终止。另见Sync, Async and Promises

    因此,在您的示例中,您可以通过发送响应来结束请求:

    exports.add = functions.https.onRequest((request, response)=>
    {
        var ref = admin.database().ref("jobs");
        var childRef = ref.push();
        childRef.set
        (
            {
                title: "test",
                pay: 100
            }
        );
        response.status(200).send("OK!");
    })
    

    【讨论】:

      猜你喜欢
      • 2018-08-09
      • 1970-01-01
      • 2022-01-04
      • 1970-01-01
      • 2021-01-13
      • 1970-01-01
      • 2019-10-11
      • 1970-01-01
      相关资源
      最近更新 更多