繁体   English   中英

问题 nestjs - Nest 无法解析 AuthenticationService 的依赖项(?)

[英]problem nestjs - Nest can't resolve dependencies of the AuthenticationService (?)

应用程序在启动时立即停止,其他模块尚未检查,因此错误不太可能在其中。

认证服务.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 */ ]
  })

我不明白错误,导出并提供身份验证服务。 我不在其他模块中使用身份验证服务。

我所有的服务都使用mongodb model。因此,您需要在模块和服务中进行以下操作。

用户.module.ts

@Module({
*imports: [MongooseModule.forFeature([{ name: 'User', schema: UserSchema }])],*
  providers: [UserService],
  exports: [UserService],
})
export class UsersModule {}

用户服务.ts

@Injectable()
export class UserService implements IUserService {
  constructor(
*@InjectModel(User.name) private userModel: Model<User>*
) {}

很好的例子https://wanago.io/2021/08/23/api-nestjs-relationships-mongodb/

你的 repo 中的代码甚至无法编译,所以几乎无法帮助你。 缺少东西,ts-errors 等。

但据我所知:

  • UsersModule 有提供者UserService但它没有导出它。 因此没有其他模块可以访问它。 您还需要导出 UserService。
  • AuthModule 不导入 UserService(也没有导出它),因此在 Nest 尝试匹配注入时找不到它。 您可以做的是在 AuthModule 中添加imports: [UserModule] (以及在 UserModule 中导出 UserService)。

并再次阅读模块和示例项目的文档,了解导入、导出、控制器和提供程序是如何工作的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM