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

问题描述 投票:5回答:1

我正在使用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)

任何想法,关于是什么原因以及如何解决它?

nestjs typeorm
1个回答
0
投票

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

在我的情况下,使用yarn why typeorm显示我正在安装两个不同的版本。其中一个用于注册实体,而框架使用另一个版本连接到SQL数据库,生成此冲突。

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

© www.soinside.com 2019 - 2024. All rights reserved.