简体   繁体   中英

NestJs can't resolve dependencies, why?

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 {}

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.

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 {}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM