簡體   English   中英

nestjs和typeorm-無法設置依賴項注入

[英]nestjs and typeorm - failing to setup the dependency injection

我正在嘗試使用nestjstypeorm編寫基本的CRUD應用程序,但是我無法使依賴項注入正常工作。 我試圖將代碼設置數據庫放入單獨的模塊中,然后將其導入。

這是我得到的錯誤:

[ExceptionHandler] Nest無法解析QuestionController(?)的依賴項。 請確保索引[0]的參數在當前上下文中可用。 + 14ms 4:v8 :: internal :: MaybeHandle v8 :: internal::(匿名命名空間):: HandleApiCallHelper(v8 :: internal :: Isolate *,v8 :: internal :: Handle,v8 :: internal :: Handle, v8 :: internal :: Handle,v8 :: internal :: Handle,v8 :: internal :: BuiltinArguments)[/ usr / local / bin / node]

這是基本的代碼結構:

database.module.ts

@Module({
  imports: [
    TypeOrmModule.forRoot({
      type: 'sqlite',
      database: 'database.sqlite',
    }),
  ],
})
export class DatabaseModule {
}

question.module.ts

@Module({
  imports: [
    DatabaseModule,
    TypeOrmModule.forFeature([Question]),
  ],
  providers: [QuestionDal, QuestionLogic],
})
export class QuestionModule {
}

app.module.ts

@Module({
  imports: [QuestionModule],
  controllers: [QuestionController],
  providers: [],
})
export class AppModule {
}

question.dal.ts

@Injectable()
export class QuestionDal {

  constructor(@InjectRepository(Question) private questionRepo: Repository<Question>) { }

}

question.logic.ts

@Injectable()
export class QuestionLogic {
  constructor(private questionDal: QuestionDal) { }
}

question.controller.ts

@Controller()
export class QuestionController {
  constructor(private readonly appService: QuestionLogic) { }

}

感謝您的幫助或提示

你必須出口QuestionLogic提供商內部QuestionModule

您將能夠將其注入導入QuestionModule其他模塊中。

@Module({
  imports: [
    DatabaseModule,
    TypeOrmModule.forFeature([Question]),
  ],
  providers: [QuestionDal, QuestionLogic],
  exports: [QuestionLogic] 
})
export class QuestionModule {
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM