我是Node.js的新手,我正在编写代码以通过post方法更新Firebase中的详细信息,同时更新时我会得到
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
at ServerResponse.setHeader (_http_outgoing.js:526:11)
at ServerResponse.header (G:\xampp_new\htdocs\saiby\public\node_modules\express\lib\response.js:771:10)
at ServerResponse.send (G:\xampp_new\htdocs\saiby\public\node_modules\express\lib\response.js:170:12)
at done (G:\xampp_new\htdocs\saiby\public\node_modules\express\lib\response.js:1008:10)
at tryHandleCache (G:\xampp_new\htdocs\saiby\public\node_modules\ejs\lib\ejs.js:261:5)
at View.exports.renderFile [as engine] (G:\xampp_new\htdocs\saiby\public\node_modules\ejs\lib\ejs.js:461:10)
at View.render (G:\xampp_new\htdocs\saiby\public\node_modules\express\lib\view.js:135:8)
at tryRender (G:\xampp_new\htdocs\saiby\public\node_modules\express\lib\application.js:640:10)
at Function.render (G:\xampp_new\htdocs\saiby\public\node_modules\express\lib\application.js:592:3)
at ServerResponse.render (G:\xampp_new\htdocs\saiby\public\node_modules\express\lib\response.js:1012:7)**
我一步一步地完成了调试,我唯一能发现的就是在执行Firebase更新功能之前,nodejs到达/执行到功能底部,然后又回到了Firebase功能中。
我已经附上了调试步骤,输出的内容可供参考,任何人都可以指出此代码出了什么问题。 TIA
app.post('/update_project', function(req, res) {
console.log(" update_project ");
var project_id = req.body.project_id;
var project_name = req.body.project_name;
var project_survey_Date = req.body.project_survey_Date;
var project_location = req.body.project_location;
var project_user_Mobile_No = req.body.project_user_Mobile_No;
var project_Assigned_to = req.body.project_Assigned_to;
var project_status = req.body.project_status;
var project_image = req.body.image;
var Mark_as_Completed = req.body.Mark_as_Completed;
if (Mark_as_Completed != undefined) {
Mark_as_Completed = 'yes';
} else {
Mark_as_Completed = 'no';
}
if (project_id != '' && project_name != '' && project_survey_Date != '' && project_location != '' && project_user_Mobile_No != '') {
var db = admin.database();
var projectsref = db.ref("projects/" + project_id);
if (project_image != undefined && project_image != '') {
projectsref.update({
project_image: project_image
});
console.log(" update_project 123");
}
console.log(" update_project 43ftgerfgref4ef");
try {
projectsref.update({
project_name: project_name,
project_Visiting_Date: project_survey_Date,
project_location: project_location,
project_user_Mobile_No: project_user_Mobile_No,
project_Assigned_to: project_Assigned_to,
project_status: project_status,
Mark_as_Completed: Mark_as_Completed,
}).then(function() {
console.log("called Then ");
return res.redirect('/projects');
}).catch(function(error) {
console.log("Data could not be saved." + error);
});
return;
} catch (error) {
console.log(" Error : 483 " + error);
}
console.log(" try catch ends");
}
console.log(" fiukll outer ");
});
输出