Javascript pdfKit "Error: write after end" due to closed stream...?

Javascript pdfKit "Error: write after end" due to closed stream...?

我正在关闭 "finally" 块中的写入连接,但即使在进入该块之后,程序似乎又回到了 try 块。 这是代码:

    function printAllAssetsAndThreats(startX, startY) {
      try {

    con.query("SELECT * FROM Kunde1Assets;", function (err, result, fields) {
      for (var i in result) {
        console.log("b");
        myDoc.font('Helvetica-Bold')
        .fontSize(20)
        .text('str', startX, startY);

        var xxx = result[i].KundenAssetID;

        startY = startY + 50;
        //jetzt jeweils alle gefährdungen:
        var sql = "SELECT DISTINCT c.AID, b.GID, b.Name, c.Name AS Asset  FROM Kunde1Verbindungen a, Gefährdungen b, Kunde1Assets c  WHERE a.KundenAssetID =  \"" + xxx + " \"AND a.GID = b.GID  AND c.KundenAssetID = a.KundenAssetID";
        con.query(sql, function (err, result2, fields) {
          for (var i2 in result2) {

          }
        });
        ///////////////////



      }
    });
  } catch (e) {
  } finally {
    console.log("a");
    end();
  }
}

所以即使在打印 "a" 之后,控制台中也会打印 "b"。 谢谢你的帮助!!!

蒂姆

编辑

我已经尝试重写代码 f.e。有回调,但它仍然无法正常工作

start(()=>{

  myDoc.end();
});

 function start(callback){
  console.log("hello");
  myDoc.font('Helvetica-Bold')
  .fontSize(20)
  .text('str', 44,44);
    con.query("SELECT * FROM Kunde1Assets;", function (err, result, fields) {
      console.log("hello");
      var counter=0;
   for(var i in result){
     console.log("hello");
     start2(result[i].KundenAssetID, ()=>{});
     counter++;
      if (counter==result.length){
     console.log("yo");break;callback();
   }
   if (counter==result.length){
    console.log("yo");callback();
  }

   }
//


      });

}
 function start2(kaid, callback){
        var sql = "SELECT DISTINCT c.AID, b.GID, b.Name, c.Name AS Asset  FROM Kunde1Verbindungen a, Gefährdungen b, Kunde1Assets c  WHERE a.KundenAssetID =  \"" + kaid + " \"AND a.GID = b.GID  AND c.KundenAssetID = a.KundenAssetID";
        con.query(sql, function (err, result2, fields) {
          for (var i2 in result2) {
            console.log(kaid +"---"+ result2[i2].Name);
            myDoc.font('Helvetica-Bold')
            .fontSize(20)
            .text('str', 44,44);
          }
        });

}

编辑: 用 promises 试了一下(还没用)

myDoc.pipe(fs.createWriteStream('node.pdf'));

var promise = start();
promise.then(function(result){

  console.log(result);
  myDoc.end();
})  
   function start(){
     return new Promise (function(resolve, reject){
      con.query("SELECT * FROM Kunde1Assets;", function (err, result, fields) {
        console.log(result);
        for (var i in result){
           myDoc.font('Helvetica-Bold')
          .fontSize(20)
          .text(result[i].Name, 30, 20+(i*30));

          var promise2 = start2 (result[i].KundenAssetID);
          promise.then(function(name){
            for (var i2 in name){
              myDoc.font('Helvetica-Bold')
              .fontSize(20)
              .text('result[i].Name', 30, 20+(i2*30));
            }
          });
        }

        resolve(result);
        if (Error) reject();
        });
     }); 
  }


   function start2(kaid){
     return new Promise(function( resolve, reject){
       var sql = "SELECT DISTINCT c.AID, b.GID, b.Name, c.Name AS Asset  FROM Kunde1Verbindungen a, Gefährdungen b, Kunde1Assets c  WHERE a.KundenAssetID =  \"" + kaid + " \"AND a.GID = b.GID  AND c.KundenAssetID = a.KundenAssetID";
          con.query(sql, function (err, result2, fields) {
            for (var i2 in result2) {
              //console.log(kaid +"---"+ result2[i2].Name);


            }
           resolve(result2);
           if (Error) reject();
          });
     });       
  }

您的 try/catch/finally 块正在同步 运行ning。它将在 JS 事件循环的第一个滴答中命中 finally。但是(我假设,基于回调的结构),con.query() 函数是异步的。这意味着它会 运行 在 至少 你的 finally 之后的勾号。因此,这将始终 运行 在 finally.

内的所有内容之后

如果您希望它按预期工作,请研究实现 try/catch/finally 结构的异步方法。关于这个,异步库,承诺等,有数百万种方法可以给猫剥皮。或者只是在连接回调中的连接后保留你想要的任何代码运行。

无论哪种方式,问题都是同步和异步代码的组合 运行彼此并排。