NestJs的新手,根据https://docs.nestjs.com/techniques/database进行配置后 无法弄清楚我的代码问题。
错误:Nest无法解析AdminRepository(?)的依赖项。请确保在TypeOrmModule上下文中可使用索引[0]处的连接Connection。
管理模块:
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>
) {}
}
应用模块:
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 {}
答案 0 :(得分:3)
您似乎在TypeOrmModule.forRoot()/forRootAsync()
中缺少AppModule
,从而建立了到数据库的初始连接。确保已将其添加到imports
数组(具有必要的配置)中,您应该会很好。
答案 1 :(得分:1)
您必须在应用模块中导入typeorm配置。请参考下面的代码。
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 {}