skip to Main Content

Im fairly new to nest.js and following along a tutorial which you can see here:

https://www.freecodecamp.org/news/build-web-apis-with-nestjs-beginners-guide/

So Im building the validate.pipe.ts and Im getting an error on the following code.

import { Injectable, ArgumentMetadata, BadRequestException, ValidationPipe, UnprocessableEntityException } from '@nestjs/common';

@Injectable()
export class ValidateInputPipe extends ValidationPipe {
   public async transform(value, metadata: ArgumentMetadata) {
      try {
        return await super.transform(value, metadata);
      } catch (e) {
         if (e instanceof BadRequestException) {
            throw new UnprocessableEntityException(this.handleError(e.message.message));
         }
      }
   }

   private handleError(errors) {
    return errors.map(error => error.constraints);
   }
}

This is my code above and my error is

Property 'message' does not exist on type 'string'.

throw new UnprocessableEntityException(this.handleError(e.message.message));

2

Answers


  1. Chosen as BEST ANSWER

    I passed the e straight in and that seemed to fix the problem

    } catch (e) {
             if (e instanceof BadRequestException) {
                throw new UnprocessableEntityException(e);
             }
          }
        ```
    

  2. it should be

    e.message

    not e.message.message in the below line

            throw new UnprocessableEntityException(this.handleError(e.message.message));
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search