fir*_*riz 7 node.js typescript typeorm nestjs
我正在尝试使用 Nestjs 构建一个应用程序,目前我有两个模块:用户和身份验证,其结构如下:
我需要将UsersService注入AuthService以便与User实体交互,因此首先我将UsersRepository注入UsersService并导出服务:
用户.模块.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 {}
Run Code Online (Sandbox Code Playgroud)
用户.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 });
}
}
Run Code Online (Sandbox Code Playgroud)
用户.存储库.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);
}
}
Run Code Online (Sandbox Code Playgroud)
问题是当我需要从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)
Run Code Online (Sandbox Code Playgroud)
我的问题是......我错过了什么,导致了这个痛苦的问题!
xap*_*hod 14
您不导出和导入存储库,而是多次“重新创建”存储库。在您想要“重新创建”存储库(即 Something.module.ts 文件)的模块中,您应该导入带有所需存储库实体的 TypeOrmModule 。
例子:
@Module({
imports: [
TypeOrmModule.forFeature([
...
MyEntity,
]),
...
Run Code Online (Sandbox Code Playgroud)
...然后在模块的服务中:
export class MyService {
constructor(
@InjectRepository(MyEntity) private repo: Repository<MyEntity>,
Run Code Online (Sandbox Code Playgroud)
zem*_*mil -1
当您创建自定义存储库扩展存储库时,您应该直接使用它,因此请尝试:
const user = await this.findOne(User, { username });
Run Code Online (Sandbox Code Playgroud)
您可以在此处找到有关自定义存储库和管理器的更多信息
| 归档时间: |
|
| 查看次数: |
16825 次 |
| 最近记录: |