Импортировать репозиторий другого модуля в мой модуль в nestjs - PullRequest
0 голосов
/ 08 ноября 2019

Я пытаюсь создать приложение с помощью Nestjs, и в настоящее время у меня есть два модуля: User и Auth со следующей структурой:

enter image description here

Мне нужновнедрить UsersService в AuthService для взаимодействия с User сущностью, поэтому сначала я внедрил UsersRepositoy в UsersService и экспортировал службу:

users.module.ts:

import { Module } from '@nestjs/common';
import { UsersService } from './users.service';
import { TypeOrmModule } from '@nestjs/typeorm';
import { UserRepository } from './user.repository';

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

users.service.ts:

import { Injectable } from '@nestjs/common';
import { AuthCredentialsDto } from '../auth/dto/auth-credentials.dto';
import { JwtPayload } from '../auth/jwt-payload.interface';
import { User } from './user.entity';
import { InjectRepository } from '@nestjs/typeorm';
import { UserRepository } from './user.repository';

@Injectable()
export class UsersService {
  constructor(
    @InjectRepository(UserRepository)
    private userRepository: UserRepository,
  ) {}

  async signUp(authCredentialsDto: AuthCredentialsDto): Promise<void> {
    return this.userRepository.signUp(authCredentialsDto);
  }

  async validateUserPassword(authCredentialsDto: AuthCredentialsDto): Promise<string> {
    return this.userRepository.validateUserPassword(authCredentialsDto);
  }

  async findOne({ username }: JwtPayload): Promise<User> {
    return this.userRepository.findOne({ username });
  }
}

users.repository.ts:

import { ConflictException, Injectable, InternalServerErrorException } from '@nestjs/common';
import * as bcrypt from 'bcrypt';
import { Repository, EntityRepository } from 'typeorm';
import { User } from './user.entity';
import { AuthCredentialsDto } from '../auth/dto/auth-credentials.dto';

@Injectable()
@EntityRepository(User)
export class UserRepository extends Repository<User> {
  async signUp(authCredentialsDto: AuthCredentialsDto): Promise<void> {
    const { username, password } = authCredentialsDto;

    const salt = await bcrypt.genSalt();
    const user = new User();
    user.username = username;
    user.password = await this.hashPassword(password, salt);
    user.salt = salt;

    try {
      await user.save();
    } catch (error) {
      if (error.code === '23505') {
        throw new ConflictException('Username already exists');
      } else {
        throw new InternalServerErrorException();
      }
    }
  }

  async validateUserPassword(authCredentialsDto: AuthCredentialsDto): Promise<string> {
    const { username, password } = authCredentialsDto;
    const user = await this.manager.findOne(User, { username });

    if (user && await user.validatePassword(password)) {
      return user.username;
    } else {
      return null;
    }
  }

  private async hashPassword(password: string, salt: string): Promise<string> {
    return bcrypt.hash(password, salt);
  }
}

Проблема в том, когда мне нужно вызвать UserService методов из AuthService , я получу ошибку в следующем формате:

[Nest] 6267   - 11/08/2019, 3:30:52 AM   [ExceptionsHandler] Cannot read property 'findOne' of undefined +4600ms
TypeError: Cannot read property 'findOne' of undefined
    at UserRepository.validateUserPassword (/home/firiz/Projects/giftos/api/dist/users/user.repository.js:35:41)
    at UsersService.validateUserPassword (/home/firiz/Projects/giftos/api/dist/users/users.service.js:26:36)
    at AuthService.signIn (/home/firiz/Projects/giftos/api/dist/auth/auth.service.js:24:49)
    at AuthController.signIn (/home/firiz/Projects/giftos/api/dist/auth/auth.controller.js:26:33)
    at /home/firiz/Projects/giftos/api/node_modules/@nestjs/core/router/router-execution-context.js:37:29
    at process._tickCallback (internal/process/next_tick.js:68:7)

Мой вопрос ... что я пропускаю, вызывая эту болезненную проблему! Спасибо заранее.

1 Ответ

0 голосов
/ 08 ноября 2019

Когда вы создаете свой собственный репозиторий, расширяет репозиторий, вы должны работать с ним напрямую, поэтому попробуйте:

const user = await this.findOne(User, { username });

Более подробную информацию о пользовательском репо и менеджере вы можете найти здесь

...