#NestJS Dependency Resolution Failure

1 messages · Page 1 of 1 (latest)

cyan surge
#

i checked my AppModule and AdoptionModule configurations but I'm still unable to resolve the issue Any suggestions on how to fix this ?

my files code below

#

app.module.ts

  import { Module } from "@nestjs/common";

import { TypeOrmModule } from "@nestjs/typeorm";

import { AdoptionController } from "./adoption/adoption.controller";
import { AdoptionService } from "./adoption/adoption.service";
import { AdoptionModule } from "./adoption/adoption.module";

@Module({
  imports: [
    TypeOrmModule.forRoot({
      type: "mysql",
      host: "localhost",
      port: 3306,
      username: "root",
      password: "",
      database: "animals_adoption",
      autoLoadEntities: true,
      synchronize: true,
    }),
    AdoptionModule,
  ],
  controllers: [AdoptionController],
  providers: [AdoptionService],
})
export class AppModule {}

adoption.module.ts

import { Module } from "@nestjs/common";
import { AdoptionService } from "./adoption.service";
import { AdoptionController } from "./adoption.controller";
import { TypeOrmModule } from "@nestjs/typeorm";
import { Adoption } from "./entities/adoption.entity";

@Module({
  imports: [TypeOrmModule.forFeature([Adoption])],
  providers: [AdoptionService],
  controllers: [AdoptionController],
})
export class AdoptionModule {}

#

adoption.service.ts

  import { HttpStatus, Injectable, NotFoundException } from "@nestjs/common";
import { InjectRepository } from "@nestjs/typeorm";
import { Adoption } from "./entities/adoption.entity";
import { Repository } from "typeorm";
import { CreateAdoptionDto } from "./dto/create-adoption.dto";

@Injectable()
export class AdoptionService {
  constructor(
    @InjectRepository(Adoption)
    private readonly AdoptionRepository: Repository<Adoption>,
  ) {}

  findAll(): Promise<Adoption[]> {
    return this.AdoptionRepository.find();
  }

  async findOne(id: number) {
    const Adoption = await this.AdoptionRepository.findOne({ where: { id } });
    if (!Adoption) {
      throw new NotFoundException(
        ` Adoption with id = ${id} ${HttpStatus.NOT_FOUND} `,
      );
    }
    return Adoption;
  }

  // async create(createAdoptionDto: CreateAdoptionDto) {
  //   const Adoption = this.AdoptionRepository.create(createAdoptionDto)
  //   return this.AdoptionRepository.save(Adoption)
  // }
  //
  // update(id: number, updatedAdoption: Partial<Adoption[]>): Promise<void> {
  //   return;
  // }
  //
  async delete(id: number) {
    const Adoption = await this.findOne(id);
    return this.AdoptionRepository.remove(Adoption);
  }
}

mild finch
#

Right, so your mistake is registering AdoptionService and AdoptionController twice in the application. Don't worry, this is a typical beginner's mistake when working with NestJS. Any time (more or less), when you put a thing in providers or controllers, you tell Nest to "provide an instance of this thing at runtime". But since you registered it both in AppModule and in AdoptionModule, it tries to instantiate it in both places. Since there is no AdoptionReposiory (made available by TypeOrmModule.forFeature([Adoption])) inside AppModule, an injection error is thrown.

elder hillBOT
#

Don't list a service in providers array of a module unless the module really provides the service.
If your ServiceA depends on a ServiceB from different module ModuleB, add ServiceB to ModuleBs exports array,
then add ModuleB to imports array in your module.

@Module({
  providers: [ServiceB],
  exports: [
    ServiceB // <-- export the "ServiceB" provider by adding it to the module's exports array
  ], 
})
export class ModuleB {}
@Module({
  imports: [
    ModuleB // <-- modules that wish to inject the "ServiceB" will need to import the "ModuleB" in their imports array
  ],  
  providers: [ServiceA],
})
export class ModuleA {}
import { ServiceB } from '../module-b/service-b.service'

@Injectable()
export class ServiceA {
  constructor(private readonly serviceB: ServiceB) {}
  
  // async foo() {
  //   this.serviceB.bar();
  // }
}
cyan surge