我遇到了这个麻烦:
[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 并停止工作。我已经delay
从tsrynge
但仍然无法正常工作。
当我调用使用此存储库的路由时会发生这种情况,但看起来不错,数据库已创建。
我在应用程序文件中的导入:
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";