很酷的钢笔 · 《花花公子》模特COS塞尔达公主,你喜欢么?· 4 月前 · |
近视的春卷 · 江苏省人民政府 部门资讯 ...· 4 月前 · |
冷静的茶壶 · sana动漫全集《哥布林的洞窑樱花》动漫完整 ...· 8 月前 · |
任性的围巾 · 【教程】百度网盘小程序如何获取真实的链接和提 ...· 1 年前 · |
暴躁的电影票 · 陈德旺 - 福州大学 - 数学与计算机科学学院· 1 年前 · |
堆栈=> Nodejs,Express.js,Typeorm
我试着在打字稿上用打字机。我创建了一个存储库来保存幸存者信息。但是我不断地发现这个错误:
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是这样的:
{
"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)
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)
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‘中的一个文件中,这个文件是:
import { createConnection } from 'typeorm'
createConnection()
然后在这个名为server.ts的文件中导入连接:
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}`))
我真的被困住了
发布于 2021-02-13 17:23:08
基本上- TypeORM尝试获取“默认”连接选项,但如果找不到,则抛出一个错误:
ConnectionNotFoundError:没有找到连接“默认”。
在建立连接之前,我们可以通过执行某种
检查
来解决这个问题--幸运的是,我们在
getConnectionManager()
上有了
.has()
方法。
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);