gpt4 book ai didi

typescript - NestJs 无法解析依赖,为什么?

转载 作者:行者123 更新时间:2023-12-04 16:27:33 25 4
gpt4 key购买 nike

NestJs 新手,根据 https://docs.nestjs.com/techniques/database 配置后
无法弄清楚我的代码的问题。

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.



管理模块:
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 {}

管理员 Controller :
import { Controller } from '@nestjs/common';

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

管理服务:
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 {}

最佳答案

你好像失踪了 TypeOrmModule.forRoot()/forRootAsync()在您的 AppModule它建立到数据库的初始连接。确保已添加到 imports阵列(具有必要的配置),你应该很高兴。

关于typescript - NestJs 无法解析依赖,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60251250/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com