작업 : 데이터베이스에서 쿼리 가져 오기 => 여러 pdf => 노드 js를 통해 메일 첨부 파일로 보내기 문제점 : pdfs가 생성되기 전에 문제가 발생했습니다. 내 sendMail() 함수가 호출되었습니다. . 그래서 모든 PDF 파일이 생성 된 후에 만 sendMail 함수를 호출하는 방법은 무엇입니까?노드 js가 첨부 파일을 동봉하여 pdf로 이메일 보내기
pdfs = [];
// Pdf generate code
var generatePdf = function (rows) {
return new Promise(function (resolve, reject) {
for (var i = 0; i < rows.length; i++) {
html = ejs.renderFile(
'views/voucher.ejs',
{
voucher: rows[i]
},
function (error, success) {
if (error) {
console.log(error);
} else {
var pdf_path = '/srv/ voucher/uploads/voucher/' + Math.random().toString(36).substring(7) + '.pdf';
htmlToPdf.convertHTMLString(success, pdf_path, function (error, success) {
if (error) {
console.log('Oh noes! Errorz!');
console.log(error);
} else {
pdfs.push(pdf_path);
console.log("Single");
console.log(pdfs);
console.log('Woot! Success!');
}
});
}
});
}
resolve();
});
}
// send mail code
var sendMail = function() {
new Promise(function (resolve, reject) {
console.log("All" + pdfs);
pdfs.forEach(function (value, key) {
// mail sending code
});
});
};
var findVoucherAndSendMail = function() {
return new Promise(function (resolve, reject) {
var query = con.query('SELECT * FROM voucher limit 20', function selectAll(err, rows, fields) {
if (err) {
throw err;
}
resolve(rows);
});
});
};
findVoucherAndSendMail().then(function (fromResolve) {
return generatePdf(fromResolve);
}).then(function() {
return sendMail();
}).catch(function() {
});
이 문제가 해결되지 않습니다하지만 당신은 내가 당신의 코드를 테스트하고 문제를 재현 할 수없는 센드 메일 – MatthieuLemoine
의 약속을 반환하지 않습니다. 전에 sendMail을 호출 했습니까? – MatthieuLemoine
예, pdf 생성에 pdf 생성 코드를 쓰지 않았습니다. 약 3 ~ 5 초가 걸리므로 sendmail은 pdf 생성 전에 호출됩니다. –