admin管理员组文章数量:1334942
I am just getting started with NestJS and stuck with a very common issue it seems but still not able to get past it. Not sure what I'm doing wrong. Sharing the exception and the codes below. Kindly help me understand what's wrong and how to fix it.
Error Nest can't resolve dependencies of the TodoService (?). Please make sure that the argument Todo at index [0] is available in the TodoModule context.
app.module.ts
import { Module } from '@nestjs/common';
import { TodoModule } from './module/todo.module';
@Module({
imports: [TodoModule],
})
export class AppModule {}
todo.module.ts
import { Module } from '@nestjs/common';
import { DatabaseModule } from '../module/database.module';
import { TodoController } from '../controller/todo.controller';
import { EntityProvider } from '../provider/todo.provider';
import { TodoService } from '../service/todo.service';
@Module({
imports: [DatabaseModule],
controllers: [TodoController],
providers: [...EntityProvider,TodoService],
})
export class TodoModule {}
todo.service.ts
import { Injectable, Inject } from '@nestjs/common';
import { Repository } from 'typeorm';
import { Todo } from '../entity/todo.entity';
@Injectable()
export class TodoService {
constructor(
@Inject(Todo)
private readonly todoRepository: Repository<Todo>
) {}
findAll(): Promise<Todo[]> {
return this.todoRepository.find();
}
async remove(id: number): Promise<void> {
await this.todoRepository.delete(id);
}
async create(task: Todo): Promise<Todo> {
return this.todoRepository.save(task);
}
async update(task: Todo): Promise<Todo> {
return this.todoRepository.save(task);
}
}
database.module.ts
import { Module } from '@nestjs/common';
import { databaseProviders } from '../provider/database.providers';
@Module({
providers: [...databaseProviders],
exports: [...databaseProviders],
})
export class DatabaseModule {}
database.provider.ts
import { DataSource } from 'typeorm';
export const databaseProviders = [
{
provide: 'DATA_SOURCE',
useFactory: async () => {
const dataSource = new DataSource({
type: 'postgres',
host: 'xxxxxxxxxxxxxx',
port: 5432,
username: 'xxxxxxxxxxxxx',
password: 'xxxxxxx',
database: 'postgres',
entities: ["dist/**/*.entity{.ts,.js}"],
synchronize: true,
ssl: {
rejectUnauthorized: false
}
});
return dataSource.initialize();
},
},
];
I am just getting started with NestJS and stuck with a very common issue it seems but still not able to get past it. Not sure what I'm doing wrong. Sharing the exception and the codes below. Kindly help me understand what's wrong and how to fix it.
Error Nest can't resolve dependencies of the TodoService (?). Please make sure that the argument Todo at index [0] is available in the TodoModule context.
app.module.ts
import { Module } from '@nestjs/common';
import { TodoModule } from './module/todo.module';
@Module({
imports: [TodoModule],
})
export class AppModule {}
todo.module.ts
import { Module } from '@nestjs/common';
import { DatabaseModule } from '../module/database.module';
import { TodoController } from '../controller/todo.controller';
import { EntityProvider } from '../provider/todo.provider';
import { TodoService } from '../service/todo.service';
@Module({
imports: [DatabaseModule],
controllers: [TodoController],
providers: [...EntityProvider,TodoService],
})
export class TodoModule {}
todo.service.ts
import { Injectable, Inject } from '@nestjs/common';
import { Repository } from 'typeorm';
import { Todo } from '../entity/todo.entity';
@Injectable()
export class TodoService {
constructor(
@Inject(Todo)
private readonly todoRepository: Repository<Todo>
) {}
findAll(): Promise<Todo[]> {
return this.todoRepository.find();
}
async remove(id: number): Promise<void> {
await this.todoRepository.delete(id);
}
async create(task: Todo): Promise<Todo> {
return this.todoRepository.save(task);
}
async update(task: Todo): Promise<Todo> {
return this.todoRepository.save(task);
}
}
database.module.ts
import { Module } from '@nestjs/common';
import { databaseProviders } from '../provider/database.providers';
@Module({
providers: [...databaseProviders],
exports: [...databaseProviders],
})
export class DatabaseModule {}
database.provider.ts
import { DataSource } from 'typeorm';
export const databaseProviders = [
{
provide: 'DATA_SOURCE',
useFactory: async () => {
const dataSource = new DataSource({
type: 'postgres',
host: 'xxxxxxxxxxxxxx',
port: 5432,
username: 'xxxxxxxxxxxxx',
password: 'xxxxxxx',
database: 'postgres',
entities: ["dist/**/*.entity{.ts,.js}"],
synchronize: true,
ssl: {
rejectUnauthorized: false
}
});
return dataSource.initialize();
},
},
];
Share
Improve this question
edited Nov 22, 2024 at 12:19
Shivayan Mukherjee
asked Nov 21, 2024 at 5:40
Shivayan MukherjeeShivayan Mukherjee
8273 gold badges12 silver badges36 bronze badges
5
|
1 Answer
Reset to default 0it's pretty clear that you're trying to inject a provider with the token Todo
in TodoService
but there are no provider registered in the TodoModule
module nor in DatabaseModule
module with such token. I don't know what you're trying to do exactly but judging by the type you have in there, I think you should be using @InjectRepository(Todo)
instead.
That's well documented in providers and database pages.
本文标签: typescriptUnable to resolve module service dependency in NestJSStack Overflow
版权声明:本文标题:typescript - Unable to resolve module service dependency in NestJS - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742312111a2451077.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
DatabaseModule
. It looks like it didn't export any provider with theTodo
token. – Micael Levi Commented Nov 21, 2024 at 15:22@Inject(Todo)
should be@InjectRepository(Todo)
instead – Micael Levi Commented Nov 21, 2024 at 15:23DatabaseModule
. So share the value ofdatabaseProviders
as well – Micael Levi Commented Nov 21, 2024 at 19:56