2017-03-19 18 views
0

したがって、get要求を処理する高速ルーティングを使用しています。 getリクエストは、電子メールの送信を処理する関数を呼び出すことになります。私はファイルのいずれかが読み込まれない場合、関数が続行しないようにする方法を理解しようとしています。メイン関数のコールバック関数からの戻り値

ルーティングロジック:

app.get('/example',sendEmail,function(req,res){ 
    //Code that runs before calling my sendEmail function 

    var results = sendEmail("example.com"); 

    // Check if sending the email was successful or threw an error. 
}); 

メール/ EJSロジック:

function sendEmail(emailAddress){ 
    var emailTEXT = ejs.renderFile('TEXT.ejs', function(err,file){ 
     if(err){ 
      console.log(err); 
      // break out of the sendEmail function by returning data 
     } else { 
      return file // Return this value to the variable and continue on with the function. 
     } 
    ); 
    var emailHTML = ejs.renderFile('HTML.ejs', function(err,file){ 
     if(err){ 
      console.log(err); 
      // break out of the sendEmail function by returning data 
     } else { 
      return file // Return this value to the variable and continue on with the function. 
     } 
    ); 

    // Code to send email with the HTML and TEXT version of the email. 
} 

私はエラー時に空の文字列を返し、各変数の後にそれをチェックすることも可能だろうが、より簡単が存在しなければなりませんこれを達成する方法

答えて

0

このようなものを試すことができます:

function sendEmail(emailAddress){ 
    var emailTEXT = new EJS({url: 'TEXT.ejs'}).render(/* Some data */); 
    var emailHTML = new EJS({url: 'HTML.ejs'}).render(/* Some data */); 

    // Code to send email with the HTML and TEXT version of the email. 
} 
+0

ノードバージョンのejsを使用しているので、「EJS」が定義されていないため、これは機能しないと思われます。私は 'var ejs = require( 'ejs');をファイル内にも持っています。私は明日の朝にこれを試して、それがうまくいくかどうかを見てみましょう – Michael