Patrick Lumenus
Patrick Lumenus

Reputation: 1722

NestJS Unable to Resolve Dependencies

I'm getting this error when I run my NestJS app.

[Nest] 19139   - 03/01/2020, 2:10:01 PM   [ExceptionHandler] Nest can't resolve dependencies of the AccountsService (AccountRepository, ?, HashPasswordService). Please make sure that the argument Object at index [1] is available in the AccountsModule context.

Potential solutions:
- If Object is a provider, is it part of the current AccountsModule?
- If Object is exported from a separate @Module, is that module imported within AccountsModule?
  @Module({
    imports: [ /* the Module containing Object */ ]
  })
 +1ms

I am a bit confused what it causing this. As far as I can tell, my code looks correct. Here is the definition for my AccountsService class:

import { Injectable, ConflictException, Logger, InternalServerErrorException, NotFoundException, Inject } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Account } from 'src/accounts/entities/account';
import { Repository, FindManyOptions, UpdateDateColumn } from 'typeorm';
import { CreateAccount } from 'src/accounts/dtos/create-account';
import { GetAccountsWithFilters } from 'src/accounts/dtos/get-accounts-with-filters';
import { UpdateAccountProfileInfo } from 'src/accounts/dtos/update-account-profile-info';
import { HashPasswordService } from '../hash-password/hash-password.service';
import { UpdateEmail } from 'src/accounts/dtos/update-email';
import { UpdatePhone } from 'src/accounts/dtos/update-phone';
import { AccountRepository } from 'src/accounts/repositories/account-repository';


/**
 * AccountsService encapsulates all the actions that can be performed by an account.
 */
@Injectable()
export class AccountsService {

    constructor(
        @InjectRepository(AccountRepository) private accountRepository: AccountRepository,
        private logger = new Logger("Accounts Service"),

        @Inject(HashPasswordService)
        private hashPasswordService: HashPasswordService,
    ) { }
    // more code here
}

My Module looks like this.

import { Module } from '@nestjs/common';
import { AccountsService } from './services/accounts/accounts.service';
import { TypeOrmModule } from '@nestjs/typeorm';
import { Account } from './entities/account';
import { AccountsController } from './controllers/accounts/accounts.controller';
import { AccountCleanerService } from './services/account-cleaner/account-cleaner.service';
import { AuthenticationService } from './services/authentication/authentication.service';
import { AuthenticationController } from './controllers/authentication/authentication.controller';
import { HashPasswordService } from './services/hash-password/hash-password.service';
import { JwtModule } from "@nestjs/jwt";
import { PassportModule } from "@nestjs/passport";
import { JwtStrategy } from './auth-strategies/jwt-strategy';
import { AccountRepository } from './repositories/account-repository';

@Module({
  imports: [
    TypeOrmModule.forFeature([
      Account,
      AccountRepository,
    ]),
    JwtModule.register({
      secret: "SOME_APP_SECRET",
      signOptions: {
        expiresIn: 3600
      }
    }),
    PassportModule.register({
      defaultStrategy: "jwt",
    }),
  ],
  controllers: [
    AccountsController, 
    AuthenticationController,
  ],
  providers: [
    AccountRepository,
    HashPasswordService,
    AccountsService, 
    AccountCleanerService, 
    AuthenticationService,  
    JwtStrategy,
  ],
  exports: [JwtStrategy, PassportModule],
})
export class AccountsModule { }

Lastly, here is the App Module:

import { Module } from '@nestjs/common';
import { AccountsModule } from './accounts/accounts.module';
import { TypeOrmModule } from "@nestjs/typeorm";
import {Account} from "./accounts/entities/account";
import { ConfigModule } from "@nestjs/config";
import account from "./../config/account";
import auth from "./../config/auth";
import database from "./../config/database";
import server from "./../config/server";
import { AccountRepository } from './accounts/repositories/account-repository';

@Module({
  imports: [
    AccountsModule,
    ConfigModule.forRoot({
      // make this module available globally
      isGlobal: true,

      // The configuration files.
      load: [
        account,
        auth,
        database,
        server
      ],
    }),
    TypeOrmModule.forRoot({
      type: "mongodb",
      url: "my connection string here",
      entities: []
    }),
  ],
  controllers: [],
  providers: [],
})
export class AppModule { }

As you can see, I have clearly made the services available to the module. So, I am kind of confused why Nest is unable to resolve the dependencies. Additionally, there should not be any other module right now, aside from the App module.,, which is also provided above. Any ideas why NestJS is throwing this error?

Upvotes: 1

Views: 4978

Answers (1)

Jay McDoniel
Jay McDoniel

Reputation: 70540

Nest is having trouble resolving the dependency of Logger, and as it is not provided in the providers array, it won't be able to resolve it. You've got three options:

1) Move the private logger = new Logger('Account Service') to the body of the constructor

2) Move the private logger = new Logger('Account Service') to the third position and mark it as @Optional() so that Nest doesn't throw an error when the value is unknown.

3) Add Logger to the providers array of AccountModule and then use the this.logger.setContext() method to properly set the context

The built in Logger class is @Injectable() so it is possible to use it through DI, but you have to ensure that it is provided just as any other provider is in the NestJS ecosystem.

Upvotes: 2

Related Questions