2016-10-25 1 views
1

こんにちは私はMySQLデータベースのデータを使ってPDFファイルを作成しようとしていますが、スクリプトを実行するとPDFファイルの作成が終了し、それへのデータ.....私はノードのMySQLが非同期だからだと思います。これに対する解決策はありますか? ここに私のコードは...あるpdf作成時のノード内の同期mysqlクエリ

var project_ID = 1; 
var PDFDocument = require('pdfkit'); 
$('#pdf').click(function(e) { 
    e.preventDefault(); 
    var pdf = new PDFDocument; 
    pdf.pipe(fs.createWriteStream(__dirname + '/../MyFile.pdf')); 
    var option; 
    switch ($("input[name=report-type]:checked").val()) { 
     case 'all-issues': 
      option = {project_id: project_ID}; 
      break; 

     case 'all-issues-customer': 
      option = {project_id: project_ID, customer: customer}; 
      break; 
    } 
    connection.getConnection(function (err, conn) { //make connection to DB 
     if (err) { //error handling 
      showNotification('error connecting: ' + err.stack, 'danger', 'glyphicon glyphicon-tasks'); 
      return; 
     } 
     conn.query('SELECT issues.id, issues.dbid , issues.date, issues.charm , issues.defect ,issues.key , issues.status, issues.summary, issues.description , actions.date as action_date, actions.description as action_desc FROM issues' + 
      ' INNER JOIN actions on issues.id = actions.issue_id WHERE ? ', [option], function (error, data) { 
      if (error) { 
       showNotification('Error :' + error, 'danger', 'glyphicon glyphicon-tasks'); 
      } else { 
       var lastID = -1; 
       data.forEach(function (data) { 
        if (lastID !== data.id) { 
         lastID = data.id; 
         pdf.addPage(); 
         pdf.text('Number :' + data.dbid).moveDown(); 
         pdf.text('Customers :   '); 
         pdf.text('Baseline :   '); 
         pdf.text('Error/Wish :   ' + data.key).moveDown(); 
         pdf.text('Charm : ' + data.charm + '/Defect : ' + data.defect + '     Status : ' + data.status); 
         pdf.text('Summary :    ' + data.summary); 
         pdf.text('Description :   ' + data.description).moveDown(); 
         pdf.text('Actions/ History :').moveDown(); 
        } 
        pdf.text('   - date  : ' + convertDate(data.date)); 
        pdf.text('   description : ' + data.description).moveDown(); 
       }) 
      } 
     }); 
     conn.release(); 
    }); 
    pdf.end(); 
}); 

答えて

0

それは理由Node.js.の非同期の性質のものですあなたはすぐにそれを修正するためにいくつかの領域であなたのコードを変更することができます。 conn.release()pdf.end()のように、のコールバックがコールバックの外にあるとします。

以下のコードでは、conn.release()pdf.end()をmysql接続コールバックの最後の行に移動しました。

var project_ID = 1; 
var PDFDocument = require('pdfkit'); 
$('#pdf').click(function(e) { 
    e.preventDefault(); 
    var pdf = new PDFDocument; 
    pdf.pipe(fs.createWriteStream(__dirname + '/../MyFile.pdf')); 
    var option; 
    switch ($("input[name=report-type]:checked").val()) { 
     case 'all-issues': 
      option = {project_id: project_ID}; 
      break; 

     case 'all-issues-customer': 
      option = {project_id: project_ID, customer: customer}; 
      break; 
    } 
    connection.getConnection(function (err, conn) { //make connection to DB 
     if (err) { //error handling 
      showNotification('error connecting: ' + err.stack, 'danger', 'glyphicon glyphicon-tasks'); 
      return; 
     } 
     conn.query('SELECT issues.id, issues.dbid , issues.date, issues.charm , issues.defect ,issues.key , issues.status, issues.summary, issues.description , actions.date as action_date, actions.description as action_desc FROM issues' + 
      ' INNER JOIN actions on issues.id = actions.issue_id WHERE ? ', [option], function (error, data) { 
      if (error) { 
       showNotification('Error :' + error, 'danger', 'glyphicon glyphicon-tasks'); 
      } else { 
       var lastID = -1; 
       data.forEach(function (data) { 
        if (lastID !== data.id) { 
         lastID = data.id; 
         pdf.addPage(); 
         pdf.text('Number :' + data.dbid).moveDown(); 
         pdf.text('Customers :   '); 
         pdf.text('Baseline :   '); 
         pdf.text('Error/Wish :   ' + data.key).moveDown(); 
         pdf.text('Charm : ' + data.charm + '/Defect : ' + data.defect + '     Status : ' + data.status); 
         pdf.text('Summary :    ' + data.summary); 
         pdf.text('Description :   ' + data.description).moveDown(); 
         pdf.text('Actions/ History :').moveDown(); 
        } 
        pdf.text('   - date  : ' + convertDate(data.date)); 
        pdf.text('   description : ' + data.description).moveDown(); 
       }) 
      } 
      conn.release(); 
      pdf.end(); 
     }); 
    }); 
}); 

コールバックロジックが完了するまでこれらの呼び出しが行われないようにする必要があります。 注:コールバック内にコールバック(またはそれ以上)があった場合、それらのコールバック内でこれらの関数が呼び出されることがよくあります。 Callback Hellの真ん中にいることがよくあります。

あなたに最高の運があります!

+1

ありがとうございました.....私はpdf.end()を内部で動かしていました。 –

+0

あなたは大歓迎です:) – Ding

関連する問題