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
  • please show us the DatabaseModule. It looks like it didn't export any provider with the Todo token. – Micael Levi Commented Nov 21, 2024 at 15:22
  • also, as you can find in the docs, I believe that @Inject(Todo) should be @InjectRepository(Todo) instead – Micael Levi Commented Nov 21, 2024 at 15:23
  • @MicaelLevi updated the code section with database module. Also changing Inject to InjectRepository didn't help – Shivayan Mukherjee Commented Nov 21, 2024 at 17:00
  • we need to know if you're exporting the expected providers at DatabaseModule. So share the value of databaseProviders as well – Micael Levi Commented Nov 21, 2024 at 19:56
  • @MicaelLevi added the database provider – Shivayan Mukherjee Commented Nov 22, 2024 at 12:19
Add a comment  | 

1 Answer 1

Reset to default 0

it'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