0

Error

[Nest] 20898 - 07/23/2023, 6:09:36 PM ERROR [ExceptionHandler] Nest can't resolve dependencies of the UsersLineService (UsersLineRepository, HttpService, ?). Please make sure that the argument dependency at index [2] is available in the AuthModule context.

Potential solutions:

  • Is AuthModule a valid NestJS module?
  • 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?

UsersLine Module in error

import { Module } from '@nestjs/common';
import { UsersLineService } from './users-line.service';
import { UsersLineController } from './users-line.controller';
import { UsersLine } from './entities/users-line.entity';
import { TypeOrmModule } from '@nestjs/typeorm';
import { HttpModule, HttpService } from '@nestjs/axios';
import { AuthService } from 'src/auth/auth.service';

@Module({
  controllers: [UsersLineController],
  providers: [UsersLineService, AuthService, HttpService],
  imports: [ 
    TypeOrmModule.forFeature([UsersLine]),
    HttpModule
  ],
  exports: [TypeOrmModule]
})
export class UsersLineModule {}

UsersLine Service in error

import { HttpException, Injectable } from '@nestjs/common';
import { CreateUsersLineDto } from './dto/create-users-line.dto';
import { UpdateUsersLineDto } from './dto/update-users-line.dto';
import { UsersLine } from './entities/users-line.entity';
import { Repository } from 'typeorm';
import { InjectRepository } from '@nestjs/typeorm';
import dataSource from 'config/database';
import { catchError, map } from 'rxjs';
import { HttpService } from '@nestjs/axios';
import { AuthService } from 'src/auth/auth.service';

@Injectable()
export class UsersLineService {
  constructor(
    @InjectRepository(UsersLine)
    private usersLineService: Repository<UsersLine>,
    private readonly httpService: HttpService,
    private readonly authService:AuthService,
  ) {}

  create(createUsersLineDto: CreateUsersLineDto) {
    return this.usersLineService.save(createUsersLineDto);
  }
  
  findOne(id: number): Promise<UsersLine | null> {
    return this.usersLineService.findOneBy({ id });
  }

  async checkUserTrue(employee_id: string, shop_id:number) {
    const data = await this.usersLineService.find({ where: { employee_id : employee_id , shop_id: shop_id}})
    if(data.length !=0){
      return 200;
    }else{
      return 401;
    }
  }

  async checkUserTrues(line_id:string, shop_id:number, employee_id: string) {
    const data = await this.usersLineService.findOne({ where: { employee_id : employee_id , shop_id: shop_id, line_id:line_id}})
    if(data){
      return 200;
    }else{
      return 401;
    }
  }

  async verifyUser(employee_id: string, line_id:string) {
    const dataIn = await this.usersLineService.findOne({ where: { employee_id : employee_id , line_id: line_id, }})
    if(dataIn){
      const data = {
        "status" : 200,
        "data" : dataIn
      }
      return data;
    }else{
      const data = {
        "status" : 401,
        "data" : "Not have"
      }
      return data;
    }
  }

  async approveRegister(userId: number, approveBy:number) {
    const dataInTime = new Date;
    const user = await this.authService.getToken(); 
    console.log(user);

    const dataIn = await this.usersLineService
    .createQueryBuilder()
    .update(UsersLine)
    .set({
      active: 2,
      edit_by: approveBy,
      last_update: dataInTime
    })
    .set({ 
      active: 2, 
      edit_by: approveBy,
      last_update: dataInTime
     })
    .where({ id: userId })
    .execute()
    
    if(dataIn){
      const headersRequest = {
        //"authorization": `Bearer ${user.access_token}`,
        "authorization": `Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOjUsInR5cGUiOiJwbGF0Zm9ybSIsImlhdCI6MTY5MDEwOTIzMywiZXhwIjoxNjkwMTEyODMzfQ.JlDWT8qgMAC2ODciVubZewKTyMxspfxkrZCli7DntjA`,
      };
      return this.httpService
        .post("http://localhost:3000/api-call-in/approveRegister", 
        {
            "shop_id" : 5,
            "product_id" : 4853775
  
        },{
          headers: headersRequest,
        })
        .pipe(map((response) => {
          console.log(response.status);
          const data = {
            "status" : 200,
            "data" : dataIn
          }
          return data
        }))
        .pipe(
          catchError(error => {
            console.log(error.response.status);
            throw new HttpException(error.response.status, error.response.status);
          }),
        );
    }else{
      const data = {
        "status" : 401,
        "data" : "Not have"
      }
      return data;
    }
  }
  

}

Auth Module in error

import { Module } from '@nestjs/common';
import { AuthService } from './auth.service';
import { UsersModule } from '../users/users.module';
import { JwtModule } from '@nestjs/jwt';
import { AuthController } from './auth.controller';
import { jwtConstants } from './constants';
import { UsersLineService } from 'src/users-line/users-line.service';
import { TypeOrmModule } from '@nestjs/typeorm';
import { UsersLine } from 'src/users-line/entities/users-line.entity';
import { PlatformId } from 'src/platform-id/entities/platform-id.entity';
import { PlatformIdService } from 'src/platform-id/platform-id.service';
import { HttpModule, HttpService } from '@nestjs/axios';

@Module({
  imports: [
    HttpModule,
    UsersModule,
    JwtModule.register({
      global: true,
      secret: jwtConstants.secret,
      signOptions: { expiresIn: '60m' },
    }),
    TypeOrmModule.forFeature([UsersLine,PlatformId]),
  ],
  providers: [AuthService, UsersLineService, PlatformIdService, HttpService],
  controllers: [AuthController],
  exports: [TypeOrmModule, HttpModule],
})
export class AuthModule {}

0 Answers0