iT邦幫忙

0

node 的 write after end

  var FirstString_Ar=feeback.pathname.split("/");            // 沒有第二筆資料進入  FirstString_Ar[1] 指令
  ShortCommadSucess_fg=false;
  console.log("ShortCommadSucess_fg ="+ShortCommadSucess_fg);
    console.log("FirstString_Ar[0] = "+FirstString_Ar[0])
    console.log("FirstString_Ar[1] = "+FirstString_Ar[1])
  for(a=0 ; a<=Shortcommad_ay.length;a++)
  {
    if(FirstString_Ar[1]===Shortcommad_ay[a])
    {
      if(FirstString_Ar[1]==="MachineTemperature")
      {
          var fs = require('fs')
        ShortRootPath_A="C:\\Agilent_i1000\\Report\\MQTT log\\";
		      ShortRootPath_A=ShortRootPath_A+"MachineTemperature.json";			
        fs.stat(ShortRootPath_A, function(err, stat)                      // 
        {
          if(stat&&stat.isFile()) 
          {           
            fs.readFile(ShortRootPath_A, function (err, data)
            {
              console.log(data.toString());
              ShortCommadSucess_fg=true;
              res.write(data);
              ShortRootPath_A=null;
              res.end();         
              console.log("ShortCommadSucess_fg ="+ShortCommadSucess_fg);
            });
          }  
          else 
          {
            console.log("400 Bad Request. The < "+ShortRootPath_A+" > not found.");
            res.write("400 Bad Request. The < "+ShortRootPath_A+" > not found.");
            res.end();         
          }
        }) //*/	
      }
      if(FirstString_Ar[1]==="MachineConfiguration")
      {
          var fs = require('fs')
		      console.log("in")
        ShortRootPath_A="C:\\Agilent_i1000\\Report\\MQTT log\\";
        ShortRootPath_A=ShortRootPath_A+"MachineConfiguration.json"
        console.log("ShortRootPath_A ="+ShortRootPath_A);
        fs.stat(ShortRootPath_A, function(err, stat)                      // 
        {
          if(stat&&stat.isFile()) 
          {           
            fs.readFile(ShortRootPath_A, function (err, data)
            {
              console.log(data.toString());
              res.write(data);
              ShortRootPath_A=null;

// ShortCommadSucess_fg=true;
res.end();
});
}
else
{
console.log("400 Bad Request. The < "+ShortRootPath_A+" > not found.");
res.write("400 Bad Request. The < "+ShortRootPath_A+" > not found.");
res.end();
}
}) ///
}
if(FirstString_Ar[1]==="DiagnosticTestResults")
{
var fs = require('fs')
ShortRootPath_A="C:\Agilent_i1000\Report\MQTT log\";
ShortRootPath_A=ShortRootPath_A+"DiagnosticTestResults.json"
console.log("ShortRootPath_A ="+ShortRootPath_A);
fs.stat(ShortRootPath_A, function(err, stat) //
{
if(stat&&stat.isFile())
{
fs.readFile(ShortRootPath_A, function (err, data)
{
console.log(data.toString());
res.write(data);
ShortRootPath_A=null;
// ShortCommadSucess_fg=true;
res.end();
});
}
else
{
console.log("400 Bad Request. The < "+ShortRootPath_A+" > not found.");
res.write("400 Bad Request. The < "+ShortRootPath_A+" > not found.");
res.end();
}
}) //
/
}
}
}
console.log("ShortCommadSucess_fg ="+ShortCommadSucess_fg);
/* if(ShortCommadSucess_fg===false)
{
console.log("400 Bad Request. The < "+FirstString_Ar[1]+" > not found.");
// res.write("400 Bad Request. The < "+FirstString_Ar[1]+" > not found.");
res.end();
} //*/
}
ShortCommadSucess_fg=false;
}
}).listen(1337);

程式碼如上,我要讓for裡面的條件如果都不符合(即ShortCommadSucess_fg都沒=true),要輸出最下方mark起來的部分if(ShortCommadSucess_fg===false)
但我只要取消隱藏,就會在命令提是字元里顯示 write after end 可否請高手教導我,我到底哪裡有少寫,或哪裡沒寫好,

看更多先前的討論...收起先前的討論...
dance0000 iT邦新手 5 級 ‧ 2018-09-17 14:23:04 檢舉
僅需看一個if即可,其他為重複條件判斷。
抱歉補充一點,當錯誤顯示時,要顯示在瀏覽器上(res.write),及console.log出來
froce iT邦大師 5 級 ‧ 2018-09-17 14:47:49 檢舉
把整個code弄在code block好嗎?
這樣很難看懂。
另外 write after end 是你在res.end()後,又用了res.write()產生的錯誤。
所以一定是前面某個if block出問題。

還有,這code重複的地方太多,有些東西應該拿出來弄成function,要不然以後很難維護。
dance0000 iT邦新手 5 級 ‧ 2018-09-17 16:59:53 檢舉
謝謝您,我再改改
fillano iT邦超人 1 級 ‧ 2018-09-18 09:15:58 檢舉
res.end()要在確定沒有要寫入res後再呼叫,你在for裡面一直呼叫他幹麼XD

尚未有邦友回答

立即登入回答