Home > other >  Nest can't resolve dependencies of the service?
Nest can't resolve dependencies of the service?

Time:07-28

I'm trying access a database using a custom provider as per this guide. At startup, Nestjs throws the error Nest can't resolve dependencies of the EventsService (?). Please make sure that the argument DATA_SOURCE at index [0] is available in the AppModule context.

Here are my files

Database providers

import { DataSource } from 'typeorm';

export const databaseProviders = [
    {
        provide: 'DATA_SOURCE',
        useFactory: async () => {
            const dataSource = new DataSource({
                type: "mysql",
                host: "host",
                port: 3306,
                username: "username",
                password: "password",
                synchronize: true,
                logging: true,
            });

            return dataSource.initialize();
        },
    },
];

Database module

import { databaseProviders } from "./database.providers";
import { Module } from "@nestjs/common";

@Module({
    providers: [...databaseProviders],
    exports: [...databaseProviders],
  })
  export class DatabaseModule {}

Events service

import { Inject, Injectable } from '@nestjs/common';
import { DataSource } from 'typeorm';
import { DatabaseModule } from './database.module';
import { Event } from './entities/event.entity';
import { EventInvite } from './entities/eventInvite.entity';

@Injectable()
export class EventsService {

    constructor(@Inject("DATA_SOURCE") private readonly database: DataSource) { }

    createEvent(userId: string, event: Event) {
        this.database.manager.create(Event, event)
    }

    deleteEvent(eventId: string){
        this.database.manager.delete(Event, { eventId })
    }
}

Events Module

    import { Module } from '@nestjs/common';
    import { DatabaseModule } from './database.module';
    import { EventsController } from './events.controller';
    import { EventsService } from './events.service';
    
    @Module({
      imports: [DatabaseModule],
      controllers: [EventsController],
      providers: [EventsService],
      exports: [EventsService]
    })
    export class EventsModule {}

App module

import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { EventsController } from './events/events.controller';
import { EventsService } from './events/events.service';
import { EventsModule } from './events/events.module';
import { DatabaseModule } from './events/database.module';

@Module({
  imports: [],
  controllers: [AppController, EventsController],
  providers: [AppService, EventsService],
})
export class AppModule {}

If I import DatabaseModule inside of AppModule everything works. My question is, why is this required? My understanding thus far is that Nestjs builds a dependency tree, which in this case should look something like AppModule => EventService => DatabaseService. AppModule doesn't directly access DatabaseService, and therefore shouldn't need to import it directly, so why is Nestjs failing to resolve this dependency?

CodePudding user response:

that module isn't global, thus its providers aren't globally available. As you're registering the service EventsService again in AppModule, you need to import the DatabaseModule

I believe this is what you're trying to do (which is pretty much what the docs shows):

@Module({
  imports: [EventsModule],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

than you won't have 2 instances of EventsService and EventsController anymore, only the one registered in EventsModule module.

  • Related