gpt4 book ai didi

javascript - firebase.update() 导致 nodejs 服务器崩溃 : Can't set headers after they are sent

转载 作者:搜寻专家 更新时间:2023-11-01 00:07:10 25 4
gpt4 key购买 nike

我正在尝试从 nodejs 中的 server.js 文件中更新 firebase 位置。使用来自 firebase api 的 .update() 会导致应用程序崩溃并抛出错误“发送后无法设置 header ”。如果我注释掉更新 firebase 位置的代码,则没有错误。有谁知道这是什么原因?

导致错误的代码:

app.post('/updateFire', function(req,res){
var updateInfo = req.body;
console.log(updateInfo);
var onComplete = function(error) {
if (error) {
console.log('Synchronization failed');
} else {
console.log('Synchronization succeeded');
res.json('done');
}
};
ref.update({points:updateInfo.points,totalAmount:updateInfo.totalAmount}, onComplete);
});

但是,如果您取出该代码并向客户端返回一个简单的响应,它不会抛出错误:

app.post('/updateFire', function(req,res){
var updateInfo = req.body;
console.log(updateInfo);
res.json('done');
});

导致崩溃的 firebase .update() 是什么?

从 Firebase 控制台更新错误

我猜控制台正在从 firebase 输出错误:

FIREBASE WARNING: Exception was thrown by user callback. Error: Can't set headers after they are sent.
at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:335:11)
at ServerResponse.header (/Users/chaselester/Sites/Development/chore-tracker/chore-angular/node_modules/express/lib/response.js:695:10)
at ServerResponse.send (/Users/chaselester/Sites/Development/chore-tracker/chore-angular/node_modules/express/lib/response.js:146:12)
at ServerResponse.json (/Users/chaselester/Sites/Development/chore-tracker/chore-angular/node_modules/express/lib/response.js:235:15)
at /Users/chaselester/Sites/Development/chore-tracker/chore-angular/app.js:36:13
at /Users/chaselester/Sites/Development/chore-tracker/chore-angular/node_modules/firebase/lib/firebase-node.js:49:642
at Ab (/Users/chaselester/Sites/Development/chore-tracker/chore-angular/node_modules/firebase/lib/firebase-node.js:46:165)
at wb (/Users/chaselester/Sites/Development/chore-tracker/chore-angular/node_modules/firebase/lib/firebase-node.js:22:216)
at xb (/Users/chaselester/Sites/Development/chore-tracker/chore-angular/node_modules/firebase/lib/firebase-node.js:21:1260)
at Qh.g.update (/Users/chaselester/Sites/Development/chore-tracker/chore-angular/node_modules/firebase/lib/firebase-node.js:204:471)
/Users/chaselester/Sites/Development/chore-tracker/chore-angular/node_modules/firebase/lib/firebase-node.js:46
ion(){P("Exception was thrown by user callback.",b.stack||"");throw b;},Math.f

堆栈跟踪错误:

stack trace:  Error
at /Users/chaselester/Sites/Development/chore-tracker/chore-angular/app.js:56:17
at Layer.handle [as handle_request] (/Users/chaselester/Sites/Development/chore-tracker/chore-angular/node_modules/express/lib/router/layer.js:82:5)
at next (/Users/chaselester/Sites/Development/chore-tracker/chore-angular/node_modules/express/lib/router/route.js:110:13)
at Route.dispatch (/Users/chaselester/Sites/Development/chore-tracker/chore-angular/node_modules/express/lib/router/route.js:91:3)
at Layer.handle [as handle_request] (/Users/chaselester/Sites/Development/chore-tracker/chore-angular/node_modules/express/lib/router/layer.js:82:5)
at /Users/chaselester/Sites/Development/chore-tracker/chore-angular/node_modules/express/lib/router/index.js:267:22
at Function.proto.process_params (/Users/chaselester/Sites/Development/chore-tracker/chore-angular/node_modules/express/lib/router/index.js:321:12)
at next (/Users/chaselester/Sites/Development/chore-tracker/chore-angular/node_modules/express/lib/router/index.js:261:10)
at /Users/chaselester/Sites/Development/chore-tracker/chore-angular/node_modules/express/lib/router/index.js:603:15
at next (/Users/chaselester/Sites/Development/chore-tracker/chore-angular/node_modules/express/lib/router/index.js:246:14)

最佳答案

根据我的经验(同样的错误),您必须在代码中的某处使用 .on('value',fn) 方法从 Firebase 读取数据。您应该将其更改为 .once('value',fn)。因为 .on('value',fn) 方法让您在 Firebase 上的 .update() 数据时返回并可能再次尝试 res.send({}) 。这就是为什么您收到“发送后无法设置 header ”错误的原因。

关于javascript - firebase.update() 导致 nodejs 服务器崩溃 : Can't set headers after they are sent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31906323/

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