在Nestjs的模块中导入另一个模块的存储库

时间:2019-11-08 00:28:31

标签: node.js typescript nestjs typeorm

我正在尝试使用Nestjs构建应用程序,目前我有两个模块:具有以下结构的用户和身份验证:

enter image description here

我需要将 UsersService 注入到 AuthService 中,以便与 User 实体进行交互,因此我首先注入了 UsersRepository < / strong>进入 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);
  }
}

问题是当我需要从 AuthService 调用 UserService 方法时,出现以下格式的错误:

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

我的问题是...我所缺少的是什么,导致了这个痛苦的问题!

2 个答案:

答案 0 :(得分:0)

创建自定义存储库扩展存储库时,应直接使用它,因此请尝试:

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

您可以找到more about custom repo and manager here

答案 1 :(得分:0)

您不导出和导入存储库,而是多次“重新创建”存储库。在您想要“重新创建”repo 的模块(即 something.module.ts 文件)中,您应该使用您想要的 repo 实体导入 TypeOrmModule。

示例:

@Module({
  imports: [
    TypeOrmModule.forFeature([
      ...
      MyEntity,
    ]),
    ...

...然后在模块的服务中:

export class MyService {
  constructor(
    @InjectRepository(MyEntity) private repo: Repository<MyEntity>,