在尝试从云功能访问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:UsersThugmAppDataRoamingnpmnode_modulesfirebase-toolsnode_modulesexpresslibresponse.js:730:10)
at ServerResponse.send (C:UsersThugmAppDataRoamingnpmnode_modulesfirebase-toolsnode_modulesexpresslibresponse.js:170:12)
at ServerResponse.json (C:UsersThugmAppDataRoamingnpmnode_modulesfirebase-toolsnode_modulesexpresslibresponse.js:256:15)
at ProxyServer.Supervisor._proxy.on (C:UsersThugmAppDataRoamingnpmnode_modulesfirebase-toolsnode_modules@google-cloudfunctions-emulatorsrcsupervisorsupervisor.js:104:14)
at ProxyServer.emit (C:UsersThugmAppDataRoamingnpmnode_modulesfirebase-toolsnode_moduleseventemitter3index.js:144:27)
at ClientRequest.proxyError (C:UsersThugmAppDataRoamingnpmnode_modulesfirebase-toolsnode_moduleshttp-proxylibhttp-proxypassesweb-incoming.js:156:18)
at emitOne (events.js:115:13)
at ClientRequest.emit (events.js:210:7)
如果有人甚至可以将我指向找到解决方案的正确方向,那将不胜感激。欢呼!
带有HTTP功能,您不会像其他类型的功能那样返回承诺。当您完全发送响应到客户端时,HTTP功能将终止。
尝试使用response.send("")
之类的内容在读取数据后用空响应完成功能:
exports.createNewGame = functions.https.onRequest((request, response) => {
admin.database().ref().once('value').then(function (data) {
console.log("BLA");
response.send("");
});
});