3

我遇到了这个麻烦:

[ERROR] 14:07:18 Error: Cannot inject the dependency at position #0 of "ListInterviewPagesUseCase" constructor. Reason: TypeInfo not known for "InterviewPagesRepository"

容器

import { container } from "tsyringe";

import { InterviewPagesRepository } from "../../modules/interview/repositories/implementations/InterviewPagesRepository";

import "./providers";

container.registerSingleton(
  "InterviewPagesRepository",
  InterviewPagesRepository
);

控制器

import { Request, Response } from "express";
import { container } from "tsyringe";

import { ListInterviewPagesUseCase } from "./ListInterviewPagesUseCase";

class ListInterviewPagesController {
  async handle(request: Request, response: Response): Promise<Response> {
    const listInterviewPagesUseCase = container.resolve(
      ListInterviewPagesUseCase
    );

    const interviewPages = await listInterviewPagesUseCase.execute();

    return response.json(interviewPages);
  }
}

export { ListInterviewPagesController };

用例

import { inject, injectable } from "tsyringe";

import { InterviewPage } from "../../entities/InterviewPage";
import { IInterviewPagesRepository } from "../../repositories/IInterviewPagesRepository";

@injectable()
class ListInterviewPagesUseCase {
  constructor(
    @inject("InterviewPagesRepository")
    private interviewPagesRepository: IInterviewPagesRepository
  ) {}

  async execute(): Promise<InterviewPage[]> {
    const interviewPages = await this.interviewPagesRepository.all();

    return interviewPages;
  }
}

export { ListInterviewPagesUseCase };

它在 Windows 上正常工作,但现在我已更改为 mac 并停止工作。我已经delaytsrynge但仍然无法正常工作。

当我调用使用此存储库的路由时会发生这种情况,但看起来不错,数据库已创建。

我在应用程序文件中的导入:

import "reflect-metadata";
import "dotenv/config";
import "./database";
import "./shared/container";

import cors from "cors";
import express from "express";
import swaggerUi from "swagger-ui-express";

import { router } from "./routes";
import swaggerFile from "./swagger.json";
4

1 回答 1

3

InterviewPagesRepository考虑用装饰@singleton,而不是称container.registerSingleton自己为:

import { singleton } from "tsyringe";

@singleton()
class InterviewPagesRepository {

@singleton装饰器确实调用registerSingleton()inside ,就像你做的那样,但它也将它存储在其他构造函数的参数中(参见12,然后是345)。

于 2021-07-03T22:17:07.880 回答