gpt4 book ai didi

node.js - 无法在位置 #0 注入(inject)依赖项,因为 TypeInfo 未知

转载 作者:行者123 更新时间:2023-12-04 07:28:01 26 4
gpt4 key购买 nike

我遇到了这个问题:

[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
);

Controller

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就不行了。我已经从 tsrynge 中设置了 delay 但仍然不起作用。

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

我在应用文件中导入:

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";

最佳答案

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

import { singleton } from "tsyringe";

@singleton()
class InterviewPagesRepository {

@singleton 装饰器 does invoke registerSingleton() inside ,就像你做的那样,但它也存储它以用于其他构造函数的参数(参见 12 ,然后是 345 )。

关于node.js - 无法在位置 #0 注入(inject)依赖项,因为 TypeInfo 未知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68117566/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com