【问题标题】:Cloud Functions for Firebase: Headers Error When Trying To Read From Realtime DatabaseCloud Functions for Firebase:尝试从实时数据库读取时出现标头错误
【发布时间】:2017-09-06 20:31:44
【问题描述】:

我在尝试从云功能访问 firebase 实时数据库时遇到了这个奇怪的错误,我想不出更多关于如何修复它的想法。这是我的代码:

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

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

exports.createNewGame = functions.https.onRequest((request, response) => {

        return admin.database().ref().once('value').then(function (data) {
                console.log("BLA");
                response.end();
        });
});

还有错误:

info: User function triggered, starting execution
info: Execution took 60010 ms, finished with status: 'timeout'
info: Execution took 60046 ms, finished with status: 'crash'
error: Something went wrong with the function!
error:  Error: Can't set headers after they are sent.
    at validateHeader (_http_outgoing.js:504:11)
    at ServerResponse.setHeader (_http_outgoing.js:511:3)
    at ServerResponse.header (C:\Users\Thugm\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\express\lib\response.js:730:10)
    at ServerResponse.send (C:\Users\Thugm\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\express\lib\response.js:170:12)
    at ServerResponse.json (C:\Users\Thugm\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\express\lib\response.js:256:15)
    at ProxyServer.Supervisor._proxy.on (C:\Users\Thugm\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\@google-cloud\functions-emulator\src\supervisor\supervisor.js:104:14)
    at ProxyServer.emit (C:\Users\Thugm\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\eventemitter3\index.js:144:27)
    at ClientRequest.proxyError (C:\Users\Thugm\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\http-proxy\lib\http-proxy\passes\web-incoming.js:156:18)
    at emitOne (events.js:115:13)
    at ClientRequest.emit (events.js:210:7)

如果有人能指出我找到解决方案的正确方向,我们将不胜感激。干杯!

【问题讨论】:

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


    【解决方案1】:

    使用 HTTP 函数,您不会像使用其他类型的函数那样返回承诺。当您向客户端完全发送响应时,HTTP 函数将终止。

    在读取数据后尝试使用response.send("") 之类的方法以空响应完成您的函数:

    exports.createNewGame = functions.https.onRequest((request, response) => {
        admin.database().ref().once('value').then(function (data) {
            console.log("BLA");
            response.send("");
        });
    });
    

    【讨论】:

    • 感谢您的建议,但是同样的问题仍然存在。返回完全相同的错误消息
    • 我刚刚部署了那个确切的函数代码,我没有遇到任何问题。
    • 有意思,可能是我在电脑上本地模拟功能来测试的吧?我将尝试将其实际部署到 Google 的服务器上,看看是否会有所作为。感谢您迄今为止的帮助!
    • @SaimirSulaj 同样的问题。我正在本地测试我的功能,并且每次都崩溃。我尝试了几种方法来终止我的功能。没有任何帮助
    • 如果您的函数在部署时工作正常,但在本地仿真方面存在问题,请提交包含所有详细信息的错误。 firebase.google.com/support/contact/bugs-features
    猜你喜欢
    • 1970-01-01
    • 2021-08-14
    • 1970-01-01
    • 2022-06-14
    • 2019-10-03
    • 2019-07-18
    • 2018-06-29
    • 2023-02-23
    • 2020-12-30
    相关资源
    最近更新 更多