firebase.update()导致nodejs服务器崩溃:发送邮件后无法设置标头

时间:2015-08-09 16:26:48

标签: javascript node.js server firebase

我正在尝试从nodejs中的server.js文件中更新firebase位置。使用firebase api中的.update()会导致应用崩溃并抛出错误"在发送标题后无法设置标题"。如果我注释掉更新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 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:

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)

1 个答案:

答案 0 :(得分:0)

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