node.js - πρόβλημα nestjs - Το Nest δεν μπορεί να επιλύσει τις εξαρτήσεις του AuthenticationService (?)


Η εφαρμογή σταματά αμέσως κατά την εκκίνηση, άλλα module δεν έχουν ελεγχθεί ακόμα, οπότε είναι απίθανο το σφάλμα να προέρχεται από αυτά.

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 */ ]
  })

Δεν καταλαβαίνω το σφάλμα, εξάγω και παρέχω την υπηρεσία αυθεντικοποίησης. Δεν χρησιμοποιώ την υπηρεσία αυθεντικοποίησης σε άλλα modules.

Απάντηση #1

Όλες οι υπηρεσίες μου χρησιμοποιούν το μοντέλο mongodb. Επομένως, πρέπει να κάνετε τα εξής στα modules και στις υπηρεσίες.

user.module.ts

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

user.service.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/

Απάντηση #2

Ο κώδικας στο αποθετήριό σας δεν μπορεί καν να μεταγλωττιστεί, οπότε σχεδόν δεν μπορώ να σας βοηθήσω καθόλου. Λείπουν πράγματα, σφάλματα ts κ.λπ.

αλλά από ό,τι βλέπω:

  • Το UsersModule έχει τον provider UserService αλλά δεν τον εξάγει. Επομένως, ΚΑΝΕΝΑ ΑΛΛΟ MODULE δεν θα έχει πρόσβαση σε αυτόν. Πρέπει επίσης να εξάγετε τον UserService.
  • Το AuthModule δεν εισάγει το UserService (και τίποτα δεν το εξάγει), οπότε δεν βρίσκεται τη στιγμή που το Nest προσπαθεί να ταιριάξει τις εξαρτήσεις. Αυτό που μπορείτε να κάνετε είναι να προσθέσετε imports: [UserModule] στο AuthModule (και επίσης να εξάγετε το UserService στο UserModule).

Και διάβασε ξανά την τεκμηρίωση των μονάδων και του παραδείγματος έργου για να δεις πώς λειτουργούν οι εισαγωγές, εξαγωγές, ελεγκτές και πάροχοι.