I have been diving into the documentation provided on the NestJs website, but I've noticed that it can be a bit scattered. My goal is to retrieve an RPG Character from a Mongo database using TypeORM. Unfortunately, I seem to be running into dependency issues along the way.
The specific error message that Nest is throwing at me reads:
[ExceptionHandler] Nest can't resolve dependencies of the CharacterModule (?). Please ensure that the argument dependency at index [0] is available within the CharacterModule context.
The core snippet of code causing trouble is as follows:
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() { }
}
In my attempts to troubleshoot, I have experimented with adding
TypeOrm.forFeature([CharacterEntity], 'default')
and exporting TypeOrmModule
. Additionally, I included @Inject() private connection: Connection
in my module's constructor.
Could you point out where I might be getting tripped up?
Update - 07/26/2020
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);
});
}
}
The latest issue being thrown states
Nest can't resolve dependencies of the CharacterEntityRepository (?). Make certain that the argument Connection at index [0] is accessible in the TypeOrmModule context.
Note that TypeOrmModule.forRoot({...}) exists in the app.module.ts
.