所以我使用快速路由来处理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 :(得分: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.
}