node.js - problem nestjs - Nest kan inte lösa beroenden för AuthenticationService (?)


Applikationen stannar omedelbart vid uppstart, andra moduler har inte kontrollerats än, så felet är osannolikt i dem.

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 {}

detta fel

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

Jag förstår inte felet, exportera och ge auth-tjänst. Jag använder inte auth-tjänst i andra moduler.

Svar #1

Alla mina tjänster använder mongodb-modellen. Därför behöver du göra följande i moduler och tjänster.

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>*
) {}

bra exempel https://wanago.io/2021/08/23/api-nestjs-relationships-mongodb/

Svar #2

Koden i ditt repo kan inte ens kompileras, så jag kan nästan inte hjälpa dig alls. Det saknas saker, ts-fel osv.

men från vad jag ser:

  • UsersModule har leverantören UserService men exporterar den inte. Därför skulle INGEN ANNAN MODUL ha tillgång till den. Du behöver också exportera UserService.
  • AuthModule importerar inte UserService (och inget exporterar det) så det hittas inte när Nest försöker matcha injektioner. Vad du kan göra är att lägga till imports: [UserModule] i AuthModule (och även exportera UserService i UserModule).

Och läs igenom dokumentationen för moduler och exempelprojektet för att se hur import, export, kontroller och leverantörer fungerar.