gpt4 book ai didi

node.js - 错误 : Can't set headers after they are sent in firebase cloud function

转载 作者:搜寻专家 更新时间:2023-10-31 22:20:44 25 4
gpt4 key购买 nike

我正在为我的 android 应用程序使用 Firebase 数据库。我正在使用它的云功能功能。我有编码函数并尝试在本地机器上测试它。

我正在运行这个命令来测试它,云函数在 firebase 服务器上运行良好,而不是在本地主机上。以下命令发送错误

firebase serve --only functions

> firebase serve --only functions

=== Serving from 'D:\Firebase'...

i functions: Preparing to emulate HTTPS functions. Support for other event types coming soon.
Warning: You're using Node.js v7.10.0 but Google Cloud Functions only supports v6.11.1.
Server#addProtoService is deprecated. Use addService instead
+ functions: getUsers: http://localhost:5002/xyz/us-central1/getUsers
info: User function triggered, starting execution
info: Execution took 60022 ms, finished with status: 'timeout'
info: Execution took 60022 ms, finished with status: 'crash'
Error: Can't set headers after they are sent.
at ServerResponse.setHeader (_http_outgoing.js:371:11)
at ServerResponse.header (C:\Users\williams\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\express\lib\response.js:730:10)
at ServerResponse.send (C:\Users\williams\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\express\lib\response.js:170:12)
at ServerResponse.json (C:\Users\williams\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\express\lib\response.js:256:15)
at ProxyServer.Supervisor._proxy.on (C:\Users\williams\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\@google-cloud\functions-emulator\src\supervisor\supervisor.js:97:12)
at ProxyServer.emit (C:\Users\williams\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\eventemitter3\index.js:144:27)
at ClientRequest.proxyError (C:\Users\williams\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\http-proxy\lib\http-proxy\passes\web-incoming.js:156:18)
at emitOne (events.js:96:13)
at ClientRequest.emit (events.js:191:7)
at Socket.socketErrorListener (_http_client.js:358:9)
info: Execution took 60054 ms, finished with status: 'crash'
Error: Can't set headers after they are sent.
at ServerResponse.setHeader (_http_outgoing.js:371:11)
at ServerResponse.header (C:\Users\williams\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\express\lib\response.js:730:10)
at ServerResponse.send (C:\Users\williams\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\express\lib\response.js:170:12)
at ServerResponse.json (C:\Users\williams\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\express\lib\response.js:256:15)
at ProxyServer.Supervisor._proxy.on (C:\Users\williams\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\@google-cloud\functions-emulator\src\supervisor\supervisor.js:97:12)
at ProxyServer.emit (C:\Users\williams\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\eventemitter3\index.js:144:27)
at ClientRequest.proxyError (C:\Users\williams\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\http-proxy\lib\http-proxy\passes\web-incoming.js:156:18)
at emitOne (events.js:96:13)
at ClientRequest.emit (events.js:191:7)
at Socket.socketCloseListener (_http_client.js:334:9)

代码:

exports.getUsers = functions.https.onRequest((request, response) => {
var x = [],
similarityCount;

db
.ref("/users/" + request.query.userId)
.once("value")
.then(function(snapshot) {
var jsonObject = snapshot.val();
var basicProfileJsonObject = jsonObject.basicProfile;
for (var key in basicProfileJsonObject) {
if (utils.isNumber(basicProfileJsonObject[key])) {
x.push(basicProfileJsonObject[key]);
}
}
db.ref("/users/").once("value").then(function(snapshot) {
var y = [];
var jsonResponse = [];
snapshot.forEach(function(item) {
var user = item.val();
let userId = user.basicProfile.userId;
if (userId !== request.query.userId) {
var basicProfileJsonObject = user.basicProfile;
for (var key in basicProfileJsonObject) {
if (utils.isNumber(basicProfileJsonObject[key])) {
y.push(basicProfileJsonObject[key]);
}
}

if (request.query.algo === "cosine") {
// compute cosine value
similarityCount = cosineUtils.cosineSimilarity(x, y);
} else if (request.query.algo == "euclidean") {
// compute euclidean distance value
similarityCount = 1 / (1 + euclidean(x, y));
} else if (request.query.algo === "pearson-correlation") {
// compute pearson correlation coefficents
similarityCount = pcorr.pearsonCorrelation(x, y);
}
console.log("------------------------------------");
console.log(x);
console.log("------------------------------------");
console.log("------------------------------------");
console.log(y);
console.log("------------------------------------");
console.log("------------------------------------");
console.log(similarityCount);
console.log("------------------------------------");
jsonResponse.push(item.val());
y = [];
}
});
response.send(jsonResponse);
});
});
});

谁能帮我看看哪里出了问题?

最佳答案

这绝对是一个棘手的案例。我认为这里发生了一个复杂的错误案例,错误消息低于标准。请注意,有两行内容为“Execution take 60022 ms, finished with status: X”。我的疯狂猜测是 Cloud Function 超时(然后发送响应)但您的代码继续在后台运行。最终它尝试发送真正的响应,但响应已经发送并且 Express.js 抛出异常。

顺便说一句,此 Cloud Function 会尝试在 HTTP 请求限制期间加载应用程序中的每个用户。无论 Cloud Functions 或 Firebase Realtime Database 的速度有多快,如果您尝试在恒定超时内进行 O(N) 操作,您就会达到极限。是否可以让您的 HTTP 函数计算两个用户之间的相似度?还是在后台主动计算用户相似度并使之无效,并在您的 HTTP 函数中返回缓存值?

关于node.js - 错误 : Can't set headers after they are sent in firebase cloud function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45532895/

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