当我调用pdfCreate
时,会加载一个事件,效果很好。
然后从包含的服务中调用createIncidentReport
,这可以正常工作。
pdfStream.on('finish')
,这在控制台上运行良好。日志已写入,但邮件服务未被调用。我该如何兑现 promise ?
function pdfCreate(incidentId) {
Incident.findById(incidentId)
.then(incident => pdfService.createIncidentReport(incident))
.then(() => mailservice.sendPdfCreatedMail(incident));
}
function createIncidentReport(incident, res) {
return new Promise((resolve, reject) => {
let pdfStream = fs.createWriteStream(settings.PDF.DIRECTORY + incident.id + '.pdf');
incidentReport.pipe(pdfStream);
pdfStream.on('finish', function () {
console.log('finished writing pdf')
resolve();
});
}
}
function sendPdfCreatedMail(incident) {
console.log('mail');
let message = {
to: incident.reporter.email,
subject: 'Incident completed',
text: 'Incident has been completed.',
html: 'Incident has been completed.'
};
sendMessage(message);
}
最佳答案
好的,我找到了答案。
.then(() => mailservice.sendPdfCreatedMail(incident));
正在等待一个事件,但我的解析没有通过它,因此该函数由于缺少参数而无法解析。
通过这样做修复它:
resolve(incident);
.then(incident => mailservice.sendPdfCreatedMail(incident));
关于node.js - Promise 中的 Nodejs 事件无法解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46068635/