任務:從數據庫獲取查詢=>生成多個pdf =>通過節點js發送郵件附件 問題:這裏的問題是在生成pdf之前我的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() {
});
它不會解決您的問題,但您不會在sendMail中返回承諾 – MatthieuLemoine
我測試了您的代碼並且無法再現您的問題。你確定sendMail之前被調用過嗎? – MatthieuLemoine
是的,我沒有寫我的pdf生成代碼生成PDF功能它需要大約3至5秒,所以sendmail被稱爲pdf生成之前。 –