Node.js 无法修补 xlsx 文件

Node.js unable to PATCH an xlsx file

我有以下 Matr.xlsx 文件:

我正在尝试通过 运行 Postman 中的 PATCH 请求将 'wood' 的价格更改为 8995

这是我的代码:

const express = require("express");
const bodyParser = require("body-parser");
const app = express();
app.use(bodyParser.urlencoded({ extended: true }));

var xlsx=require("xlsx");
 
  
app.route('/articles/:id').patch(function(req,res){
  var wb=xlsx.readFile("Matr.xlsx",{cellDates:'true'});
  var ws=wb.Sheets["Sheet1"];

  var variable = xlsx.utils.decode_range(ws["!ref"])
  
 
  rowNum=variable.e.r+1 //to find total number of rows
  console.log('rowNum is :' + rowNum);
  
  for(var i=1;i<=rowNum;i++){
    var x='A'+ i; //A is the Material Column
    
    if(ws[`${x}`].v === req.params.id){ //If wood is found in the row
  
        var b='B'+i; //B is the price column
       
        ws[`${b}`].v =req.body.price;
      
         var newData=xlsx.utils.sheet_to_json(ws);
         console.log(newData);
        
         var newWB=xlsx.utils.book_new();
         var newWS=xlsx.utils.json_to_sheet(newData);
         xlsx.utils.book_append_sheet(newWB,newWS,"Sheet1");
         xlsx.writeFile(newWB,"Matr.xlsx");
         res.send("Successfully updated");
    }
    res.send('Could not update');
  }
  })
  
  app.listen(3000, function() {
    console.log("Server started on port 3000");
});

但是我收到以下错误:

Server started on port 3000 rowNum is :5 Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client at ServerResponse.setHeader (_http_outgoing.js:558:11)

因为你发送了两次响应头。

一次,当您更新文件时:

xlsx.writeFile(newWB,"Matr.xlsx");
res.send("Successfully updated");

完成循环后的另一次:

}
res.send('Could not update');

您应该添加一种机制来处理错误或一次性更新和发送响应。使用此场景可以更好地处理:

const express = require("express");
const bodyParser = require("body-parser");
const app = express();
app.use(bodyParser.urlencoded({ extended: true }));

var xlsx=require("xlsx");
 
  
app.route('/articles/:id').patch(function(req,res){
  var wb=xlsx.readFile("Matr.xlsx",{cellDates:'true'});
  var ws=wb.Sheets["Sheet1"];

  var variable = xlsx.utils.decode_range(ws["!ref"])
  
 
  rowNum=variable.e.r+1 //to find total number of rows
  console.log('rowNum is :' + rowNum);
  
  let hasBeenUpdated = false;
  for(var i=1;i<=rowNum;i++){
    var x='A'+ i; //A is the Material Column
    
    if(ws[`${x}`].v === req.params.id){ //If wood is found in the row
  
        var b='B'+i; //B is the price column
       
        ws[`${b}`].v =req.body.price;
      
         var newData=xlsx.utils.sheet_to_json(ws);
         console.log(newData);
        
         var newWB=xlsx.utils.book_new();
         var newWS=xlsx.utils.json_to_sheet(newData);
         xlsx.utils.book_append_sheet(newWB,newWS,"Sheet1");
         xlsx.writeFile(newWB,"Matr.xlsx");

         hasBeenUpdated = true;
    }
  }

  if (hasBeenUpdate) {
    res.send("Successfully updated");
  } else {
    res.send('Could not update');
  }
})
  
app.listen(3000, function() {
    console.log("Server started on port 3000");
});