簡體   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