9

New to NestJs,after the configuration according to https://docs.nestjs.com/techniques/database can't figure out the problem of my code .

Error: Nest can't resolve dependencies of the AdminRepository (?). Please make sure that the argument Connection at index [0] is available in the TypeOrmModule context.

Admin Module:

import { Module } from '@nestjs/common';
import {Admin} from "../entities/Admin";
import {TypeOrmModule} from '@nestjs/typeorm';
import {AdminService} from "./admin.service";
import {AdminController} from "./admin.controller";

@Module({
    imports:[TypeOrmModule.forFeature([Admin])],
    exports: [TypeOrmModule],
    providers: [AdminService],
    controllers: [AdminController]
})
export class AdminModule {}

AdminController:

import { Controller } from '@nestjs/common';

@Controller('admin')
export class AdminController {}

AdminService:

import { Injectable } from '@nestjs/common';
import { Repository } from 'typeorm';
import { InjectRepository } from '@nestjs/typeorm';
import { Admin } from '../entities/Admin';


@Injectable()
export class AdminService {
    constructor(
        @InjectRepository(Admin)
        private readonly adminRepository:Repository<Admin>
    ) {}
}

App Module:

import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { LoginController } from './login/login.controller';
import { RegisterController } from './register/register.controller';
import { ContactController } from './contact/contact.controller';

import { AppService } from './app.service';
import { LoginService } from './login/login.service';
import { ContactService } from './contact/contact.service';
import { RegisterService } from './register/register.service';
import { AdminModule } from './admin/admin.module';

@Module({
  imports: [AdminModule],
  controllers: [AppController, LoginController, RegisterController, ContactController],
  providers: [AppService, LoginService, ContactService, RegisterService],
})
export class AppModule {}
2
  • Is the entity Admin decorated with @Entity decorator?
    – elderapo
    Commented Feb 16, 2020 at 19:30
  • Yes . Otherwise would be another kind of error Commented Feb 17, 2020 at 11:37

2 Answers 2

11

You have to import typeorm config in the app module. Refer to the below code.

import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { LoginController } from './login/login.controller';
import { RegisterController } from './register/register.controller';
import { ContactController } from './contact/contact.controller';

import { AppService } from './app.service';
import { LoginService } from './login/login.service';
import { ContactService } from './contact/contact.service';
import { RegisterService } from './register/register.service';
import { AdminModule } from './admin/admin.module';
import { TypeOrmModule } from '@nestjs/typeorm';


@Module({
  imports: [TypeOrmModule.forRoot(),AdminModule],
  controllers: [AppController, LoginController, RegisterController, ContactController],
  providers: [AppService, LoginService, ContactService, RegisterService],
})
export class AppModule {}

6

It looks like you are missing TypeOrmModule.forRoot()/forRootAsync() in your AppModule which sets up the initial connection to the database. Make sure that is added to the imports array (with the necessary configurations) and you should be good to go.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.