【发布时间】:2019-09-08 20:14:04
【问题描述】:
我正在使用 NestJs 创建几个应用程序,我想将代码从 NestInterceptor 移动到外部 NPM 包,以便我可以在多个应用程序中使用相同的拦截器。
问题在于,在“本地”使用时可以工作的相同代码在移动到外部包时会停止工作。
这是拦截器的代码:
import { Injectable, NestInterceptor, CallHandler, ExecutionContext } from '@nestjs/common'
import { map } from 'rxjs/operators'
import { getManager } from 'typeorm'
import jwt_decode from 'jwt-decode'
@Injectable()
export class MyInterceptor implements NestInterceptor {
entity: any
constructor(entity: any) {
this.entity = entity
}
async intercept(context: ExecutionContext, next: CallHandler): Promise<any> {
const request = context.switchToHttp().getRequest()
const repository = getManager().getRepository(this.entity)
return next.handle().pipe(map((data) => data))
}
}
这是一个给定的控制器:
import { myInterceptor } from "../src/interceptors/interceptor.ts";
@UseInterceptors(new CompanyIdInterceptor(User))
export class UserController {
}
这工作正常,但如果将文件移动到外部 NPM 包并像这样从中导入:
import { myInterceptor } from "mynpmpackage";
我收到以下错误:
[Nest] 24065 - 04/18/2019, 10:04 AM [ExceptionsHandler] Connection "default" was not found. +26114ms
ConnectionNotFoundError: Connection "default" was not found.
at new ConnectionNotFoundError (/home/andre/Services/npm-sdk/src/error/ConnectionNotFoundError.ts:8:9)
at ConnectionManager.get (/home/andre/Services/npm-sdk/src/connection/ConnectionManager.ts:40:19)
任何想法,是什么原因以及如何解决它?
【问题讨论】:
-
你使用 ts-node 吗?请注意,在节点模块中,您需要为 *.js 文件(实体和所有)定义 TypeOrm 连接,在 ts-node 中它适用于 *.ts 文件。
-
我确实使用 ts-node,但在我的 Typeorm 设置中,我确实计算了 js 文件:实体:[
${path.join(appRootPath.path, '{src,dist}')}/**/*.entity{.ts,.js}, ], -
@AndréD.Oliveira 当你将拦截器移动到外部包时,你有 typeorm 作为依赖还是对等依赖?
-
@shusson 我有 typeOrm 作为依赖和对等依赖。
-
@AndréD.Oliveira 尝试使 typeorm 仅成为外部包中的对等依赖项