写入Firebase实时数据库的云功能超时

时间:2017-11-17 11:43:11

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

我可以看到一旦获得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"}}

1 个答案:

答案 0 :(得分:2)

由HTTP请求触发的云功能需要以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!");
})