Cloud Functions for Firebase:Headers 尝试从实时数据库读取时出错
Cloud Functions for Firebase: Headers Error When Trying To Read From Realtime Database
我在尝试从云函数访问 firebase real-time 数据库时遇到了这个奇怪的错误,我想不出更多关于如何修复它的想法。这是我的代码:
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 函数,您不需要像使用其他类型的函数那样 return 承诺。当您完全向客户端发送响应时,HTTP 功能将终止。
尝试使用类似 response.send("")
的方法在读取数据后以空响应完成函数:
exports.createNewGame = functions.https.onRequest((request, response) => {
admin.database().ref().once('value').then(function (data) {
console.log("BLA");
response.send("");
});
});
我在尝试从云函数访问 firebase real-time 数据库时遇到了这个奇怪的错误,我想不出更多关于如何修复它的想法。这是我的代码:
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 函数,您不需要像使用其他类型的函数那样 return 承诺。当您完全向客户端发送响应时,HTTP 功能将终止。
尝试使用类似 response.send("")
的方法在读取数据后以空响应完成函数:
exports.createNewGame = functions.https.onRequest((request, response) => {
admin.database().ref().once('value').then(function (data) {
console.log("BLA");
response.send("");
});
});