简体   繁体   English

尝试请求存储库时未使用 TypeORM 找到连接“默认”

[英]Connection “default” was not found with TypeORM when trying to request a repository

I'm building an API with Express+TypeORM.我正在使用 Express+TypeORM 构建 API。 This is my ormconfig.json:这是我的 ormconfig.json:

{
  "type": "postgres",
  "host": "localhost",
  "port": "5432",
  "username": "mdsp9070",
  "password": "mdsp9070",
  "database": "mesha",
  "entities": ["./src/entities/*.ts"],
  "migrations": ["./src/shared/infra/typeorm/migrations/*.ts"],
  "cli": {
    "migrationsDir": "./src/shared/infra/typeorm/migrations"
  }
}

And my entity is defined as:我的实体定义为:

import {
  Entity,
  Column,
  PrimaryGeneratedColumn,
} from "typeorm";

@Entity("users")
export class User {
  @PrimaryGeneratedColumn("uuid")
  id: string;

  @Column()
  name: string;

  @Column()
  email: string;

  @Column()
  birthYear: string;

  @Column()
  phoneNumber: string;

  @Column()
  photo: string;
}

On my CreateUserUseCase, I call this repository like:在我的 CreateUserUseCase 上,我将此存储库称为:

...
private usersRepository: IUsersRepository;
  // receives ormRepository, that's users repository
  constructor() {
    this.usersRepository = getCustomRepository(UsersRepository);
  }
...

but I'm getting this error: [ERROR] 13:08:39 ConnectionNotFoundError: Connection "default" was not found.但我收到此错误: [ERROR] 13:08:39 ConnectionNotFoundError: Connection "default" was not found.

No, connection is not being called after getCustomRepository as I call the file on the index of my server.不,在我调用服务器索引上的文件时,在 getCustomRepository 之后没有调用连接。

full github repo: https://github.com/Mdsp9070/mesha完整的 github 回购: https://github.com/Mdsp9070/mesha

Explanation: Express routes eas been called before connection being established.说明:在建立连接之前调用了快速路由。 So I chose to use dynamic imports.所以我选择使用动态导入。

My application main entry was changed to typeorm/index.ts as:我的应用程序主条目更改为 typeorm/index.ts 为:

import { createConnection } from "typeorm";
import { AppError } from "../../errors/AppError";

// finds ormconfig.json file and creates a connection. (magic!),
// it could be set up manually with parameters
// however an external file is the best practise
createConnection()
  .then(() => {
     console.log("Connected to the database")
     import("../../http/server.ts")
  })
  .catch(() => new AppError("Unable to connect to the database"));

That way worked like a charm!这种方式就像一个魅力!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未使用 TypeORM 找到连接“默认” - Connection "default" was not found with TypeORM RepositoryNotFoundError:找不到“用户”的存储库。 看起来这个实体没有在当前的“默认”连接中注册? 打字机 - RepositoryNotFoundError: No repository for "User" was found. Looks like this entity is not registered in current "default" connection? Typeorm 无法使用 TypeOrm 并找不到明确的“连接”默认值“ - Cannot use TypeOrm with express “connection ”Default" not found TypeORM存储库在一个方法中找不到但在另一个方法中找不到 - TypeORM repository not found in one method but found in another 尝试使用 Postman 发出 POST 请求,但没有任何反应(NestJS 和 TypeORM) - Trying to make a POST request with Postman but nothing happens (NestJS and TypeORM) 如何在 NestJS 9 (TypeORM 3.+) 中扩展 TypeORM 存储库 - How to extend TypeORM repository in NestJS 9 (TypeORM 3.+) 尝试使用 typeorm(节点)进行迁移时出现目录错误 - Directory error when trying to do migrations using typeorm (node) 尝试使用 postgres typeorm Raw 方法时出错 - Getting an error when trying to use postgres typeorm Raw method 尝试运行迁移时的 Typeorm:缺少必需的参数:dataSource - Typeorm when trying to run migrations: Missing required argument: dataSource 如何模拟 typeorm 连接 - How to mock typeorm connection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM