[英]Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client, i can't send a form to realtime database
<form action="/user/product/add" method="POST" enctype="multipart/form-data" >
<h1 class="h">Add Product</h1>
<div>
<table class="tableez" cellpadding="4" cellspacing="0" style="width:400px">
<tr>
<div class="form-group">
<input type="file" name="imageProduct" multiple>
</div>
</tr>
<tr>
<td class="tdtitle">Tên sản phẩm: </td>
<td><input type="text" value="{{data.nameProduct}}" name="nameProduct"
id="nameProduct">
</td>
</tr>
<button type="submit" class="btn btn-primary">Thêm</button>
</form>
router.post('/product/add', upload.array('imageProduct', 5), isLoggedIn, function (req, res, next) {
var idd = "5ea4528ccaf1ab0017e0fe22";
var filesImage = req.files;
var images = [];
filesImage.forEach(function (item, index, array) {
images.unshift(item.filename);
});
var winner = [];
winner.unshift("1");
winner.unshift("Chưa có");
// var messages = [];
// messages.unshift({
// a : "qwe",
// b : "asdq"
// });
var played = [];
played.unshift("null");
var registerDatee = Date.now();
var time = req.body.time;
console.log(time);
const product = {
imageProduct: images,
nameProduct: req.body.nameProduct,
userId: idd,
nameProductType: req.body.cars,
startPriceProduct: req.body.currentPrice,
status: req.body.status,
description: req.body.description,
extraTime: req.body.time,
registerDate: registerDatee,
winner: winner,
hide: false,
currentPrice: req.body.currentPrice,
played: played
};
var db = Firebase.database();
var rootRef = db.ref('products');
rootRef.push(product);
res.redirect('/');
});
After I clicked submit, the following error occurred 3. this is my error单击提交后,出现以下错误 3. 这是我的错误
POST /user/product/add 200 151.592 ms - -
_http_outgoing.js:526
throw new ERR_HTTP_HEADERS_SENT('set');
^
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 (C:\Users\HaAnh\Desktop\api\backend-app-daugia\node_modules\express\lib\response.js:767:10)
at ServerResponse.contentType (C:\Users\HaAnh\Desktop\api\backend-app-daugia\node_modules\express\lib\response.js:595:15)
at ServerResponse.send (C:\Users\HaAnh\Desktop\api\backend-app-daugia\node_modules\express\lib\response.js:145:14)
at done (C:\Users\HaAnh\Desktop\api\backend-app-daugia\node_modules\express\lib\response.js:1004:10)
at Immediate._onImmediate (C:\Users\HaAnh\Desktop\api\backend-app-daugia\node_modules\express-handlebars\lib\utils.js:26:4)
at processImmediate (internal/timers.js:456:21) {
code: 'ERR_HTTP_HEADERS_SENT'
}
[nodemon] app crashed - waiting for file changes before starting...
I do not understand why I submitted an error after I submitted it, but it is okay to fix, I have read a lot of the above questions, but there is still no solution for my project.不明白为什么我提交后就报错了,但是改掉就好了,上面的问题看了很多,但是我的项目还是没有解决办法。 I'm crying because I don't know what to do next.
我哭了,因为我不知道下一步该做什么。 Although an error has occurred, the product has been saved in the database.
尽管发生了错误,但该产品已保存在数据库中。 Please give me a solution so I can continue working on my project!
请给我一个解决方案,以便我可以继续我的项目!
This looks to be a classic issue with non-blocking I/O in Node.这看起来是 Node.js 中非阻塞 I/O 的一个经典问题。 You will need to wrap your work in a Promise.
您需要将您的工作包装在 Promise 中。
new Promise(function(resolve, reject) {
//do the file work
var idd = "5ea4528ccaf1ab0017e0fe22";
var filesImage = req.files;
var images = [];
filesImage.forEach(function (item, index, array) {
images.unshift(item.filename);
});
// more work here
return true; //after the work is done
})
.then(() => {
// do more work here if needed
});
.then(() => {
// after the I/O work is done
res.redirect('/');
});
I didn't double check the syntax, but you can get the idea from my snippet.我没有仔细检查语法,但你可以从我的片段中得到这个想法。
I suspect that the middleware isLoggedIn
returns a response in certain cases (if you have try/catch - what are you doing in the catch?).我怀疑中间件
isLoggedIn
在某些情况下会返回响应(如果你有 try/catch - 你在 catch 中做什么?)。
These are the cases where the execution path tries to resend the response (both in the middleware as well as here in the code) and hence the error.在这些情况下,执行路径尝试重新发送响应(在中间件以及代码中)并因此出现错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.