Nest can’t resolve dependencies of the StoryController, but problem in this constructor
I really don’t unserstand where my error
In this part code Error (why I don’t understand)
more details: all work without
constructor(
@InjectRepository(StoryEvent)
private readonly storyRepository: Repository<StoryEvent>,
) {}
story.event.ts
import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm';
@Entity()
export class StoryEvent {
@PrimaryGeneratedColumn()
id: number;
@Column()
title: string;
@Column()
text: string;
@Column()
author: string;
@Column()
date: string;
}
story.controller.ts
import { Controller, Get, Param } from '@nestjs/common';
import { Repository } from 'typeorm';
import { StoryEvent } from './story.event';
import { InjectRepository } from '@nestjs/typeorm';
@Controller('/story')
export class StoryController {
constructor(
@InjectRepository(StoryEvent)
private readonly storyRepository: Repository<StoryEvent>,
) {}
@Get(':id')
getStory(@Param() params: number) {
return params;
}
}
all okay
app.module.ts
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { TypeOrmModule } from '@nestjs/typeorm';
import { StoryEvent } from './story/story.event';
import { StoryController } from './story/story.controller';
@Module({
imports: [
TypeOrmModule.forRoot({
type: 'postgres',
host: 'localhost',
port: 5432,
username: 'postgres',
password: 'EXAMPLE',
database: 'postgres',
entities: [StoryEvent],
synchronize: true,
}),
],
controllers: [AppController, StoryController],
providers: [AppService],
})
export class AppModule {}
Upd:
log in console:
[Nest] 3052 - 04.07.2023, 22:28:15 LOG [NestFactory] Starting Nest application...
[Nest] 3052 - 04.07.2023, 22:28:15 LOG [InstanceLoader] TypeOrmModule dependencies initialized +53ms
[Nest] 3052 - 04.07.2023, 22:28:15 ERROR [ExceptionHandler] Nest can't resolve dependencies of the StoryContro
ller (?). Please make sure that the argument StoryEventRepository at index [0] is available in the AppModule cont
ext.
Potential solutions:
- Is AppModule a valid NestJS module?
- If StoryEventRepository is a provider, is it part of the current AppModule?
- If StoryEventRepository is exported from a separate @Module, is that module imported within AppModule?
@Module({
imports: [ /* the Module containing StoryEventRepository */ ]
})
p.s. Thank u so much
code without error, see docs
>Solution :
Okay, so if you don’t have a StoryModule what you need to do is add TypeormModule.forFeature([StoryEvent]) into the imports of yourAppModule along with the TypeormModule.forRoot(). This creates the injection token Nest needs to know how to map the @InjectRepository(StoryEvent)