我正在关闭“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);
}
});
}
编辑: 用承诺(还没有工作)尝试了它
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();
});
});
}
答案 0 :(得分:0)
您的try/catch/finally
块正在同步运行。它将在JS事件循环的第一个刻度内点击finally
。但是(我假设,基于回调的结构),con.query()
函数是异步的。这意味着它最终将在中运行至少。因此,这将始终在finally中的所有内容之后运行。
如果您希望它按预期工作,请查看实现try/catch/finally
结构的异步方法。有一百万种方法可以对猫,异步库,承诺等进行换肤。或者只是在连接回调中的连接之后保留你想要运行的任何代码。
无论哪种方式,问题都是同步和异步代码相互并存的组合。