【问题标题】:How to fix unhandled promise rejection warning in NestJS如何修复 NestJS 中未处理的承诺拒绝警告
【发布时间】:2021-01-28 03:28:48
【问题描述】:

我将 NestJS 用于我的后端 angular 项目,并且我已经放置了方法。在我安装 swagger 之前它运行良好,但在我安装 swagger 之后,我收到一个警告,说未处理的承诺拒绝,它不会让我运行。

它工作正常如果我在控制器中评论代码,所以我认为 async/await 存在问题,但不确定如何修复它,所以如果我能获得任何关于如何修复的帮助或建议,我将不胜感激这个?

控制器

    @Put(':id')
    async updateHelpSubsection(@Body() newHelp: HelpSubsectionModule, @Param() params): Promise<HelpSubsectionModule> {
        try{
            let oldHelpData = await this.helpSubsectionService.getHelpSubsectionbyId(params.id)
            return this.helpSubsectionService.updateHelpSubsection(oldHelpData, newHelp);
        }catch(e) {
            console.log(e)
        }
    }

服务

    async updateHelpSection(updateHelp: HelpSectionEntity, newHelpData): Promise<HelpSectionEntity> {

        Object.keys(newHelpData).forEach((key) => {
            updateHelp[key] = newHelpData[key];
        });

        try {
            return await this.helpSectionRepo.save(updateHelp);
        } catch (err) {
            throw new HttpException({
                error: err
            }, HttpStatus.FORBIDDEN)
        }
    }

这是我收到的警告。

【问题讨论】:

    标签: angular typescript warnings backend nestjs


    【解决方案1】:

    不太确定@Put(:id) 是如何工作的,但是updateHelpSection 返回一个promise,所以你必须用.then() 处理它以在promise 解决时返回数据,而catch() 在promise 被拒绝时抛出错误。看看这个article

    【讨论】:

    • 嗨,您能否请您给我看一些如何用我的代码处理它的示例,因为我仍然不确定。谢谢
    • 嗨!我最近解决了这个问题,checkout this,比如app.get('/callback')方法,这里我使用了异步函数getToken,它返回promise,所以我们需要调用then()catch()来处理返回promise的resolve或者reject = > 没有错误。希望这会有所帮助,祝你好运!
    【解决方案2】:

    请在控制器中更新:

           @Put(':id')
           async updateHelpSubsection(@Body() newHelp: HelpSectionEntity, @Param() params): Promise< HelpSectionEntity> {
               try{
                   let oldHelpData = await this.helpSubsectionService.getHelpSubsectionbyId(params.id)
                   return this.helpSubsectionService.updateHelpSubsection(oldHelpData, newHelp);
               }catch(e) {
                   console.log(e)
               }
           }
    

    typescript 中的类型中不允许使用模块。

    请将 HelpSubsectionModule 替换为 HelpSectionEntity

    【讨论】:

      【解决方案3】:

      您所看到的只是对良好做法的警告。

      每个 Promise 都必须有一个方法来捕获发出请求时将产生的错误

      例如:

      这就是承诺。

      const slowAndSteady = new Promise(function(resolve, reject) {
          reject();
      });
      

      如果你像这样使用 async/await。

      (async function() {
          await slowAndSteady();
      })();
      

      您会收到 UnhandledPromiseRejectionWarning,但如果您以这种方式使用。

      slowAndSteady
          .then(function(result) {
              console.log('result', result);
          })
          .catch(function(err) {
              console.log('error: ', err);
          });
      

      警告消失。

      但如果您仍然需要使用 async/await,您有两个选择。

      (async () => {
        try {
          const result = await slowAndSteady();
          console.log('result', result);
        } catch(e) {
          console.log('Error caught',e);
        }
      })();
      

      或者

      (async () => {
        const result = await slowAndSteady().catch((e) => console.log('Error caught',e));
        console.log('result', result);
      })();
      

      【讨论】:

        猜你喜欢
        • 2020-05-18
        • 2020-01-01
        • 2018-07-02
        • 1970-01-01
        • 2020-12-24
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多