yourname
yourname

Reputation: 63

Nest JS - Cannot read properties of undefined

I got the error message [Nest] 4492 - 06/17/2022, 17:59:11 ERROR [ExceptionsHandler] Cannot read properties of undefined (reading 'findOneByEmail') when creating a custom pipe for email validation registered.

i followed the answer: Email verification with class-validator in nestJS

is there something i missed?

// email-already-exist.pipe.ts

import { Injectable } from '@nestjs/common';
import {
  registerDecorator,
  ValidationOptions,
  ValidatorConstraint,
  ValidatorConstraintInterface,
} from 'class-validator';

import { UsersService } from 'src/users/users.service';

@ValidatorConstraint({
  name: 'EmailAlreadyExist',
  async: true,
})
@Injectable()
export class EmailAlreadyExistConstraint
  implements ValidatorConstraintInterface
{
  constructor(private readonly usersService: UsersService) {}

  async validate(email: string) {
    return !(await this.usersService.findOneByEmail(email));
  }
}

export function EmailAlreadyExist(validationOptions?: ValidationOptions) {
  return function (object: any, propertyName: string) {
    registerDecorator({
      target: object.constructor,
      propertyName: propertyName,
      options: validationOptions,
      constraints: [],
      validator: EmailAlreadyExistConstraint,
    });
  };
}

// create-user.dto.ts

  @IsNotEmpty()
  @IsEmail()
  @EmailAlreadyExist({
    message: 'the email account already exist',
  })
  email: string;

// users.service.ts

import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';

import { CreateUserDto } from 'src/users/dto/create-user.dto';
import { UpdateUserDto } from 'src/users/dto/update-user.dto';
import { UserEntity } from 'src/users/entities/user.entity';

@Injectable()
export class UsersService {
  constructor(
    @InjectRepository(UserEntity)
    private usersRepository: Repository<UserEntity>,
  ) {}

  async findOneByEmail(email: string) {
    return await this.usersRepository.findOneBy({ email: email });
  }
}

// users.module.ts

import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';

import { UsersService } from 'src/users/users.service';
import { UsersController } from 'src/users/users.controller';
import { UserEntity } from 'src/users/entities/user.entity';
import { EmailAlreadyExistConstraint } from 'src/users/pipe/email-already-exist.pipe';

@Module({
  imports: [TypeOrmModule.forFeature([UserEntity])],
  controllers: [UsersController],
  providers: [UsersService, EmailAlreadyExistConstraint],
  exports: [UsersService],
})
export class UsersModule {}

and according to the answer from here TypeError: Cannot read properties of undefined on NestJS Dependency Injection I had to add @Injectable() but still not working

Upvotes: 3

Views: 22638

Answers (2)

Fredadebayo
Fredadebayo

Reputation: 21

Add the code below to the bootstrap function in your main.ts

useContainer(app.select(AppModule), { 
 fallbackOnErrors: true 
});

Upvotes: 1

GFoniX
GFoniX

Reputation: 334

Try add this in you're app.ts

useContainer(app.select(AppModule), { fallbackOnErrors: true });

source

Upvotes: 1

Related Questions