gpt4 book ai didi

javascript - Firebase 的云功能 : Headers Error When Trying To Read From Realtime Database

转载 作者:行者123 更新时间:2023-11-30 11:31:28 27 4
gpt4 key购买 nike

我在尝试从云函数访问 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)

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

最佳答案

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

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

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

关于javascript - Firebase 的云功能 : Headers Error When Trying To Read From Realtime Database,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46083862/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com