0
私にはアイテムのリストがあり、各アイテムには指示のリストがあります。各項目はPDFファイルに出力され、各命令は1ページ分を占め、ローカルに保存されます。私が今持っているのは、データベースに各項目の指示を問い合わせるforループと、PDFパイプをパスに挿入した後です。この問題は、クエリが非同期であるために、パイプされている最後のPDFのみを開くことができ、残りは壊れています。どのように私はこれを克服するのですか?私のコードは以下の通りです。PDFKITはループ内に複数のPDFファイルを配管しています
var counter = 0
for (var o=0; o<itemList.length; ++o){
Steps.find({itemid:itemid[o], sort: "sequence asc"}).exec(function(err,steps){
doc[counter] = new PDFDocument();
if(!err){
doc[counter].pipe(fs.createWriteStream(path + "DocName" + counter + ".pdf"));
for (var x=0; x<steps.length; ++x){
doc[counter].text("Instructions: " + steps[x].instruction.font('Times-Roman', 13);
}
***/*if (counter == itemList.length-1){
doc[counter].end();
}*/***
//That is the problem, i shouldn't have done the check and end the doc immediately, that solved the issue.
doc[counter].end();
++counter;
}
}
}
'callback'、' promise'、 'async/await'のいずれかを使います。人気のあるライブラリは[async](http://caolan.github.io/async/)、[bluebird](http://bluebirdjs.com/docs/getting-started.html)です。どのノードのバージョンを使用していますか? – Sangharsh
http://stackoverflow.com/q/14220321/1435132もチェックしてください – Sangharsh