[英]Unhandle Promise rejection inside try block
I'm having difficulties understanding why the code below throws an Unhandled Promise Rejection Warning :我很难理解为什么下面的代码会抛出一个未处理的承诺拒绝警告:
router.post('/stampaClasse/:as(20[0-9][0-9]/[0-9][0-9])/:classe([1-8])',async function (req,res,next){
const sezioni = await Classi.getSezioniFromAnnoScolasticoAndClasse(req.params.as,req.params.classe);
let options = {
semestre: req.body.semestre,
fontSize: req.body.fontSize,
textColor: '#515151',
gridColor: '#bec0be',
corDidattico:{
titolo:'prof.',
nome:'Roberto',
cognome:'Dalema'
}
}
let newPDF = new pdfKit();
try{
for(sezione of sezioni){
const idStudentiPromise = Classi.getStudentiFromAnnoScolasticoAndClasseAndSezione(req.params.as,req.params.classe,sezione)
const materiePromise = Classi.getMaterieSezione(req.params.as,req.params.classe,sezione)
const infoStudentiPromise = Promise.all( Studenti.getInfoStudentiById(await idStudentiResults) )
let classe = {
annoScolastico: req.params.as,
classe : req.params.classe,
sezione: sezione,
materie: await materiePromise,
studenti: await infoStudentiPromise
}
for(studente of classe.studenti){
studente.pagelleMateriePromises = classe.materie.map(async m=>Pagelle.getPagellaFromStudente(classe.annoScolastico,classe.classe,classe.sezione,m,studente.id));
}
for(studente of classe.studenti){
studente.pagelleMaterie = await Promise.all(studente.pagelleMateriePromises)
addHeader(newPDF,studente,classe,options);
addPagelleSemestre(newPDF,studente,classe,options);
addFooter(newPDF,studente,classe,options);
}
}
newPDF.pipe(res);
newPDF.end();
}
catch(err){
next(err)
}
});
The error occurs multiple times, and it is caused by the fact that at line错误发生多次,是由于在行
const infoStudentiPromise = Promise.all( Studenti.getInfoStudentiById(await idStudentiResults) )
idStudentiResults alredy returns a Promise.all() idStudentiResults 已经返回一个 Promise.all()
regardles of what is causing the error i'd like to know why it says the error is not beeing handled.不管是什么导致错误,我想知道为什么它说错误没有被处理。 Is the try catch not enaugh?
try catch 还不够吗?
Promise.all
needs to receive an array of promises as param. Promise.all
需要接收一组 promise 作为参数。 Make sure that Module.getInfo(ids)
is returning that.确保
Module.getInfo(ids)
返回那个。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.