简体   繁体   English

在 try 块内取消处理 Promise 拒绝

[英]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.

相关问题 在基本 catch 块中取消处理承诺拒绝 - Unhandle promise rejection in a basic catch block 在try / catch块上未处理的Promise拒绝 - Unhandled Promise rejection on try/catch block 即使使用 try catch 块,也会继续收到未处理的承诺拒绝 - Keep getting Unhandled promise rejection even with try catch block Redux:在 try-catch 语句上取消处理拒绝(错误) - Redux: Unhandle Rejection (Error) on a try-catch statement 代码有效,但是在try中包含try / catch块是最佳实践吗? - Code works, but is it best practice to have a try/catch block inside a promise? 未处理的 promise 拒绝。 此错误源于在没有 catch 块的情况下抛出异步 function - Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block 在Try中调用的Promise未处理的Promise拒绝 - Unhandled promise rejection from promise called within Try 未处理的 promise 拒绝与 Promise.all 解决并尝试/捕获 - Unhandled promise rejection with Promise.allSettled and try/catch 如何在 Promise 捕获块中引发异常? (未处理的 promise 拒绝) - How to throw an exception in a Promise catch block? (Unhandled promise rejection) Knexjs和es6是否尝试/捕获导致未处理的承诺拒绝警告? - Knexjs and es6 Try/Catch causing an Unhandled Promise Rejection Warning?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM