node.js - مشکل nestjs - Nest نمیتواند وابستگیهای AuthenticationService را حل کند (?)


برنامه بلافاصله در هنگام راهاندازی متوقف میشود، سایر ماژولها هنوز بررسی نشدهاند، بنابراین احتمال خطا در آنها کم است.

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);
}
}

ماژول احراز هویت.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 استفاده میکنند. بنابراین، شما نیاز دارید کارهای زیر را در ماژولها و خدمات انجام دهید.

کاربر.ماژول.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/

پاسخ شماره ۲

کد در مخزن شما حتی نمیتواند کامپایل شود، بنابراین تقریباً اصلاً نمیتواند به شما کمک کند. چیزهایی کم دارد، خطاهای ts و غیره.

اما از چیزی که من میبینم:

  • ماژول UsersModule ارائهدهنده UserService دارد اما آن را صادر نمیکند. بنابراین هیچ ماژول دیگری به آن دسترسی نخواهد داشت. شما باید UserService را نیز صادر کنید.
  • ماژول AuthModule سرویس UserService را وارد نمیکند (و هیچ چیزی آن را صادر نمیکند) بنابراین در زمان تلاش Nest برای تطبیق تزریقها پیدا نمیشود. کاری که میتوانید انجام دهید این است که imports: [UserModule] را در AuthModule اضافه کنید (و همچنین سرویس UserService را در UserModule صادر کنید).

و دوباره مستندات ماژولها و پروژه نمونه را بخوانید تا ببینید واردات، صادرات، کنترلکنندهها و ارائهدهندهها چگونه کار میکنند.