找不到连接“默认”-TypeORM,NestJS和外部NPM包

And*_*ira 5 typeorm nestjs

我正在使用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))
  }
}
Run Code Online (Sandbox Code Playgroud)

这是给定的控制器:

import { myInterceptor } from "../src/interceptors/interceptor.ts";

@UseInterceptors(new CompanyIdInterceptor(User))
export class UserController {
}
Run Code Online (Sandbox Code Playgroud)

效果很好,但是如果将文件移动到外部NPM包并从中导入,如下所示:

import { myInterceptor } from "mynpmpackage";
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

[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)
Run Code Online (Sandbox Code Playgroud)

有什么想法,是什么原因造成的以及如何解决?

Luí*_*ito 1

这可能不完全是你的问题,但我在使用 TypeORM 将内容移动到外部包时遇到了类似的问题。确保父项目中的所有包都使用相同版本的 TypeORM 包。

就我而言,使用yarn why typeorm显示正在安装两个不同的版本。其中一个用于注册实体,而框架使用另一个版本连接到 SQL 数据库,从而产生了这种冲突。

使用检查您的版本yarn why [pkg-name],或者如果您正在使用 NPM,请尝试npx npm-why [pkg-name]https://www.npmjs.com/package/npm-why进行全局安装。