簡體   English   中英

錯誤 [ERR_HTTP_HEADERS_SENT]:無法在將標頭發送到客戶端后設置標頭,我無法將表單發送到實時數據庫

[英]Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client, i can't send a form to realtime database

  1. 這是我的 html 代碼提交
<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>
  1. 這是我的 api
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('/');

});

單擊提交后,出現以下錯誤 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...

不明白為什么我提交后就報錯了,但是改掉就好了,上面的問題看了很多,但是我的項目還是沒有解決辦法。 我哭了,因為我不知道下一步該做什么。 盡管發生了錯誤,但該產品已保存在數據庫中。 請給我一個解決方案,以便我可以繼續我的項目!

這看起來是 Node.js 中非阻塞 I/O 的一個經典問題。 您需要將您的工作包裝在 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('/');
});

我沒有仔細檢查語法,但你可以從我的片段中得到這個想法。

我懷疑中間件isLoggedIn在某些情況下會返回響應(如果你有 try/catch - 你在 catch 中做什么?)。

在這些情況下,執行路徑嘗試重新發送響應(在中間件以及代碼中)並因此出現錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM