Nest无法解析RestController的依赖项

时间:2019-11-16 22:20:01

标签: javascript node.js typescript dependency-injection nestjs

我正在一个新的Nest.js项目中,并且每当我尝试启动服务器时:

void dragUpdate(DragUpdateDetails d) {
    // using d.delta you can then track the movement and implement your rotation updade here
}

我已经检查了文档并四处查看,但无法发现问题。 [ExceptionHandler] Nest can't resolve dependencies of the RestController (?). Please make sure that the argument dependency at index [0] is available in the RestModule context. 类将注入RestController,该类由UsersService导出并导入UsersModule中。一切看起来都很好,但是没有用。

这是我的代码:

RestModule

app.module.ts

import { Module } from '@nestjs/common' import { RestModule } from './rest/rest.module' @Module({ imports: [ RestModule ] }) export class AppModule { }

rest.module.ts

import { Module } from '@nestjs/common' import { RestController } from './rest.controller' import { UsersModule } from '../users/users.module' @Module({ imports: [ UsersModule ], controllers: [ RestController ] }) export class RestModule { }

rest.controller.ts

import { Controller, Inject, Get } from '@nestjs/common' import { UsersService } from '../users/users.service' import { User } from '../users/user.interface' @Controller() export class RestController { constructor ( @Inject() private readonly usersService: UsersService ) { } @Get() async getAll (): Promise<User[]> { return this.usersService.findAll() } }

users.module.ts

import { Module } from '@nestjs/common' import { UsersService } from './users.service' @Module({ providers: [ UsersService ], exports: [ UsersService ] }) export class UsersModule { }

users.service.ts

提前谢谢!

1 个答案:

答案 0 :(得分:2)

在您的RestController类中,您必须从构造函数中删除@Inject()

constructor (
  private readonly usersService: UsersService
) { }

@Inject()可用于non-class-based provider tokens