【发布时间】:2022-02-17 16:22:14
【问题描述】:
应用程序在启动时立即停止,尚未检查其他模块,因此它们不太可能出错。
authentication.service.ts
import { HttpException, HttpStatus, Inject, Injectable } from '@nestjs/common';
import { CreateUserDto } from 'src/users/dto/createUser.dto';
import { UserService } from 'src/users/user/user.service';
import bcrypt from 'bcrypt';
@Injectable()
export class AuthenticationService {
constructor(@Inject() private readonly userService: UserService){}
public async regiser(registartionData:CreateUserDto){
const hashingPassword = await bcrypt.hash(registartionData.passwordHash, 10);
try{
const createUser = await this.userService.create({
...registartionData,
passwordHash: hashingPassword
})
createUser.passwordHash = undefined;
return createUser;
}
catch(err){
throw new HttpException('Something went wrong', HttpStatus.INTERNAL_SERVER_ERROR);
}
}
public async getAuthenticationUser(email:string, textPassword:string){
try{
const user = await this.userService.findByEmail(email);
await this.verifyPassword(textPassword,user.passwordHash);
user.passwordHash = undefined;
return user;
}
catch(err){
throw new HttpException('Wrong credentials provided', HttpStatus.BAD_REQUEST);
}
}
private async verifyPassword(password:string, hashingPassword:string){
const isMatching = await bcrypt.compare(password, hashingPassword);
if(!isMatching) throw new HttpException('Wrong credentials provided', HttpStatus.BAD_REQUEST);
}
}
auth.module.ts
import { Module } from '@nestjs/common';
import { PassportModule } from '@nestjs/passport';
import { UsersModule } from 'src/users/users.module';
import { AuthController } from '../controllers/auth/auth.controller';
import { AuthenticationService } from './authentication/authentication.service';
import { LocalStrategy } from './authentication/local.strategy';
@Module({
imports:[UsersModule, PassportModule, ],
providers: [AuthenticationService, LocalStrategy],
controllers:[AuthController],
exports: [AuthenticationService]
})
export class AuthModule {}
这个错误
Nest can't resolve dependencies of the AuthenticationService (?). Please make sure that the argument dependency at index [0] is available in the AuthModule context.
Potential solutions:
- If dependency is a provider, is it part of the current AuthModule?
- If dependency is exported from a separate @Module, is that module imported within AuthModule?
@Module({
imports: [ /* the Module containing dependency */ ]
})
我不懂错误,导出并提供认证服务。我不在其他模块中使用身份验证服务。
【问题讨论】:
-
在 AuthenticationService 你有:constructor(@Inject() private readonly userService: UserService){},但在 AuthModule 中没有。您确定 UsersModule 导出 UserService 吗?
-
user.module.ts
import { Module } from '@nestjs/common'; import { UserService } from './user/user.service'; @Module({ providers: [UserService], exports: [UserService], }) export class UsersModule {} -
你为什么在没有依赖令牌的情况下使用
@Inject()? -
@Inject() 不需要,我删除了它,但是**新错误** - Nest 无法解析 UserService (?) 的依赖项。请确保索引 [0] 处的参数 [object Object] 在 UsersModule 上下文中可用。
-
@JayMcDoniel 你可以使用
@Inject与否,这真的取决于你(对于简单的注射剂,你使用它没有任何东西),但这里的问题是,没有任何 UserService 导出在模块中导入的
标签: node.js typescript nestjs