首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >ConnectionNotFoundError:找不到连接“默认”

ConnectionNotFoundError:找不到连接“默认”
EN

Stack Overflow用户
提问于 2021-02-02 13:43:53
回答 1查看 3.1K关注 0票数 1

堆栈=> Nodejs,Express.js,Typeorm

我试着在打字稿上用打字机。我创建了一个存储库来保存幸存者信息。但是我不断地发现这个错误:

代码语言:javascript
复制
ConnectionNotFoundError: Connection "default" was not found.
    at new ConnectionNotFoundError (/Users/Wblech/codeminerTeste/backend/src/error/ConnectionNotFoundError.ts:8:9)
    at ConnectionManager.get (/Users/Wblech/codeminerTeste/backend/src/connection/ConnectionManager.ts:40:19)
    at Object.getRepository (/Users/Wblech/codeminerTeste/backend/src/index.ts:284:35)
    at new CreateSurvivorRepository (/Users/Wblech/codeminerTeste/backend/src/repositories/typeorm/CreateSurvivorRepository.ts:10:26)
    at Object.<anonymous> (/Users/Wblech/codeminerTeste/backend/src/useCases/CreateSurvivor/index.ts:6:41)
    at Module._compile (internal/modules/cjs/loader.js:774:30)
    at Module._compile (/Users/Wblech/codeminerTeste/backend/node_modules/source-map-support/source-map-support.js:547:25)
    at Module.m._compile (/private/var/folders/fr/6x3p83cj6r73h57k1_x6nctc0000gn/T/ts-node-dev-hook-7748782675459163.js:69:33)
    at Module._extensions..js (internal/modules/cjs/loader.js:785:10)
    at require.extensions..jsx.require.extensions..js (/private/var/folders/fr/6x3p83cj6r73h57k1_x6nctc0000gn/T/ts-node-dev-hook-7748782675459163.js:114:20)
[ERROR] 00:28:59 ConnectionNotFoundError: Connection "default" was not found.

我的ormconfig.json是这样的:

代码语言:javascript
复制
{
  "name": "default",
  "type": "postgres",
  "host": "localhost",
  "port": 5432,
  "username": "postgres",
  "password": "teste",
  "database": "codeminer_backend",
  "entities": [
     "src/database/models/*.ts"
  ],
  "migrations": [
     "src/database/migrations/*.ts"
  ],
  "cli": {
    "migrationsDir": "./src/database/migrations"
  }
}

这是错误指定的代码:

at new CreateSurvivorRepository (/Users/Wblech/codeminerTeste/backend/src/repositories/typeorm/CreateSurvivorRepository.ts:10:26)

代码语言:javascript
复制
import { getRepository, Repository } from 'typeorm'
import { SurvivorOrm } from '../../database/models/SurvivorsOrm'
import { ICreateSurvivorRequestDTO } from '../../useCases/CreateSurvivor/CreateSurvivorDTO'
import { ICreateSurvivorRepository } from '../ICreateSurvivorRepository'

export class CreateSurvivorRepository implements ICreateSurvivorRepository {
  private ormRepository: Repository<SurvivorOrm>

  constructor () {
    this.ormRepository = getRepository(SurvivorOrm)
  }

  public async create ({ id, name, age, gender, lastLocation, infected }: ICreateSurvivorRequestDTO): Promise<SurvivorOrm> {
    const survivor = await this.ormRepository.create({ id, name, age, gender, lastLocation, infected })

    await this.ormRepository.save(survivor)

    return survivor
  }
}

at Object.<anonymous> (/Users/Wblech/codeminerTeste/backend/src/useCases/CreateSurvivor/index.ts:6:41)

代码语言:javascript
复制
import { CreateInventoryRepository } from '../../repositories/typeorm/CreateInventoryRepository'
import { CreateSurvivorRepository } from '../../repositories/typeorm/CreateSurvivorRepository'
import { CreateSurvivorController } from './CreateSurvivorController'
import { CreateSurvivorUseCase } from './CreateSurvivorUseCase'

const createSurvivorRepositoryTypeOrm = new CreateSurvivorRepository()
const createInventoryRepositoryTypeOrm = new CreateInventoryRepository()

const createSurvivorUseCase = new CreateSurvivorUseCase(
  createSurvivorRepositoryTypeOrm,
  createInventoryRepositoryTypeOrm
)

const createSurvivorController = new CreateSurvivorController(
  createSurvivorUseCase
)

export { createSurvivorUseCase, createSurvivorController }

我的连接位于路径'./src/database/index.ts‘中的一个文件中,这个文件是:

代码语言:javascript
复制
import { createConnection } from 'typeorm'

createConnection()

然后在这个名为server.ts的文件中导入连接:

代码语言:javascript
复制
import express from 'express'
import { router } from './router'
import './database'

const port = 8081

const app = express()

app.use(express.json())
app.use(router)

app.listen(port, () => console.log(`App running at ${port}`))

我真的被困住了

EN

Stack Overflow用户

发布于 2021-02-13 17:23:08

基本上- TypeORM尝试获取“默认”连接选项,但如果找不到,则抛出一个错误:

ConnectionNotFoundError:没有找到连接“默认”。

在建立连接之前,我们可以通过执行某种检查来解决这个问题--幸运的是,我们在getConnectionManager()上有了.has()方法。

代码语言:javascript
复制
import { Connection, getConnectionManager, getConnectionOptions, 
  createConnection, getConnection, QueryRunner } from 'typeorm';
    
    async init() {
    let connection: Connection;
    let queryRunner: QueryRunner;

     if (!getConnectionManager().has('default')) {
        const connectionOptions = await getConnectionOptions();
        connection = await createConnection(connectionOptions);
      } else {
        connection = getConnection();
      }

    queryRunner = connection.createQueryRunner(); 
 }
票数 1
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66010957

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档