当我尝试从云功能访问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)
如果有人能指出我找到解决方案的正确方向,我们将不胜感激。干杯!
答案 0 :(得分:3)
使用HTTP函数,您不会像使用其他类型的函数一样返回承诺。完全向客户端发送响应后,HTTP功能将终止。
尝试使用类似response.send("")
之类的内容,在读取数据后使用空响应完成您的功能:
exports.createNewGame = functions.https.onRequest((request, response) => {
admin.database().ref().once('value').then(function (data) {
console.log("BLA");
response.send("");
});
});