[英]Nest can't resolve dependencies of the RestController
我正在一個新的 Nest.js 項目中工作,每當我嘗試啟動服務器時:
[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
class 注入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
:
import { Injectable } from '@nestjs/common'
import { User } from './user.interface'
@Injectable()
export class UsersService {
async findAll (): Promise<User[]> {
return new Promise((resolve, reject) => resolve([]))
}
}
先感謝您!
在您的RestController
class 中,您必須從構造函數中刪除@Inject()
:
constructor (
private readonly usersService: UsersService
) { }
@Inject()
可用於非基於類的提供者令牌。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.