PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, April 27, 2022

[FIXED] How to fix unhandled promise rejection warning in NestJS

 April 27, 2022     angular, backend, nestjs, typescript, warnings     No comments   

Issue

I'm using NestJS for my backend angular project and I've put method. It was running fine before I install swagger but after I install swagger, I got a warning that saying unhandled promise rejection and it won't allow me to run.

It's working fine If I comment the code in controllers so I think there is a problem with async/await but not really sure how to fix it so I would be really appreciated if I can get any help or suggestion on how to fix this?

Controller

    @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)
        }
    }

Services

    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)
        }
    }

This is the warning I'm getting. enter image description here


Solution

Please update this in the controller:

       @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)
           }
       }

Modules are not allowed in the types in typescript.

Please replace HelpSubsectionModule with HelpSectionEntity.



Answered By - Praveen Kumar Palai
Answer Checked By - Mary Flores (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing