无法解决Nest.js中的模块依赖性

时间:2020-07-25 19:09:46

标签: typescript dependency-injection nestjs typeorm

我一直在NestJs网站上关注documentation,它跳了一下。。我要做的就是让它使用TypeORM从Mongo数据库中提取一个RPG角色。但是,我遇到了依赖地狱。

我从Nest收到的具体错误是

[ExceptionHandler] Nest无法解析CharacterModule(?)的依赖项。请确保在CharacterModule上下文中可以使用索引[0]处的参数依赖项。

Here is a link to the Gist

一小段代码是这样的:

character.module.ts

// tslint:disable: quotemark
import { Module, Inject } from '@nestjs/common';
import { TypeOrmModule } from "@nestjs/typeorm";
import { Connection } from 'typeorm';
import { ServeStaticModule } from '@nestjs/serve-static';
import { join } from 'path';

import { CharacterEntity } from "../entities/character.entity";
import { CharacterService } from './character.service';
import { CharacterController } from './character.controller';

@Module({
  imports: [
    TypeOrmModule
  ],
  controllers: [
    CharacterController
  ],
  providers: [
    CharacterService
  ],
  exports: [
    CharacterService,
  ]
})
export class CharacterModule {
  constructor() { }
}

我尝试添加TypeOrm.forFeature([CharacterEntity], 'default')并导出TypeOrmModule。我还向模块的构造函数中添加了@Inject() private connection: Connection

我要去哪里错了?

更新-2020年7月26日

new character.module.ts

// tslint:disable: quotemark
import { Module, Inject } from '@nestjs/common';
import { TypeOrmModule } from "@nestjs/typeorm";
import { Connection } from 'typeorm';
import { ServeStaticModule } from '@nestjs/serve-static';
import { join } from 'path';

import { CharacterEntity } from "../entities/character.entity";
import { CharacterService } from './character.service';
import { CharacterController } from './character.controller';

@Module({
  imports: [
    TypeOrmModule.forFeature([CharacterEntity])
  ],
  controllers: [
    CharacterController
  ],
  providers: [
    CharacterService
  ],
  exports: [
    CharacterService,
    TypeOrmModule
  ]
})
export class CharacterModule {
  constructor() { }
}

character.service.ts

// tslint:disable: quotemark
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Response as ExpressResponse } from 'express';

import {
 /* other imports */
    Character,
} from 'src/model';
import { CharacterEntity } from '../entities/character.entity';

@Injectable()
export class CharacterService {
    characterNotFound: NotFoundError = {
        errorType: 'Not Found',
        Event_Code: '404',
        Event_Id: '',
        Event_Message: 'The character was not found',
        Event_Subject: 'Character not found',
        Event_Timestamp: new Date().toString()
    } as NotFoundError;

    constructor(
        @InjectRepository(CharacterEntity)
        private characterRepo: Repository<CharacterEntity>
    ) { }
/* other actions */
}

app.module.ts

// tslint:disable: quotemark
// modules
import { Module } from '@nestjs/common';
import { CharacterModule } from './character/character.module';
import { CampaignModule } from './campaign/campaign.module';
import { PlayerModule } from './player/player.module';
import { SageModule } from './sage/sage.module';
import { TypeOrmModule } from "@nestjs/typeorm";
import { Connection } from 'typeorm';

// controllers
import { AppController } from './app.controller';
import { PlayerController } from './player/player.controller';
import { AuthController } from './auth/auth.controller';

// services
import { AppService } from './app.service';

// constants for everything else
import { SAGE_DB, SAGE_DB_HOST, SAGE_DB_PORT, ENTITIES_PATH } from './constants';
import { CharacterController } from './character/character.controller';
import { CharacterEntity } from './entities/character.entity';

@Module({
  imports: [
    TypeOrmModule.forRoot({
      "name": "default",
      "type": "mongodb",
      "host": SAGE_DB_HOST,
      "port": SAGE_DB_PORT,
      "database": SAGE_DB,
      "keepConnectionAlive": true,
      "synchronize": true,
      "autoLoadEntities": true
    }),
    CharacterModule,
    SageModule,
    CampaignModule,
    PlayerModule,
  ],
  controllers: [
    AppController,
    PlayerController,
    AuthController
  ],
  providers: [AppService],
})
export class AppModule {
  constructor(private connection: Connection) {
    connection.connect().then(f => {
      console.log('fulfilled', f);
    }, r => {
      console.log('rejected', r);
    });
  }
}

返回错误Nest can't resolve dependencies of the CharacterEntityRepository (?). Please make sure that the argument Connection at index [0] is available in the TypeOrmModule context. TypeOrmModule.forRoot({...})在app.module.ts中。

3 个答案:

答案 0 :(得分:0)

在您的CharacterModule中,您需要使用TypeOrmModule.forFeature([CharacterEntity]),以便为Repository(CharacterEntity)is available in the context of the CharacterModule`注入标记。

答案 1 :(得分:0)

使用存储库模式时,我发现有必要使用forRoot和forFeature来实现所需的功能。 在此示例中, ormconfig 基本上就是您已经拥有的(数据库连接设置)。 arrayofentities 是每个打字稿实体类的数组。

您当前在服务中使用存储库模式的方式也不需要您导入连接

(请注意,以下所有内容都可以放在app.module.ts中,您可以将for功能部件放入特定模块中)

imports:[
        TypeOrmModule.forRoot(ormconfig),
        TypeOrmModule.forFeature(arrayofentities)
    ]

答案 2 :(得分:0)

我也遇到了这个错误,因为我正在导入存储库和另一个前面带有“type”关键字的服务。

错误:

import type { Repository } from 'typeorm';
import type { FileUtilsService } from "../file-utils";

正确:

import { Repository } from 'typeorm';
import { FileUtilsService } from "../file-utils";