简体   繁体   English

无法使用 TypeOrm 并找不到明确的“连接”默认值“

[英]Cannot use TypeOrm with express “connection ”Default" not found

I'm currently having a bad time with typeOrm, I don't know why but express is getting initialized before my connection to the database with typeOrm So I get an error "Connection default not found"我目前在使用 typeOrm 时遇到了不好的情况,我不知道为什么,但是在我使用 typeOrm 连接到数据库之前已经初始化了 express 所以我收到一个错误“未找到连接默认值”

here's the code这是代码

Typeorm.config.ts Typeorm.config.ts

import {Connection, createConnection} from 'typeorm';

export function connectToDb(): Promise<Connection> {
    return createConnection({
        type: 'postgres',
        url: process.env.TYPEORM_URL,
        synchronize: false,
        logging: true,
        entities: [process.env.TYPEORM_ENTITIES],
        migrations: ["../../migrations/*.ts"],
        cli: {migrationsDir: process.env.TYPEORM_MIGRATIONS_DIR}
    })
}

Room.repository Room.repository

import {getRepository} from 'typeorm';
import {Room} from '../entities/Room';

const roomRepository = getRepository(Room)

export async function getAllRooms(): Promise<Room[]> {
    return await roomRepository.find()
}

this repo is used by my router, here's my app.ts这个 repo 被我的路由器使用,这是我的 app.ts

import * as express from 'express'
import * as bodyParser from 'body-parser';
import * as PassportJs from './passport';
import cors from 'cors';
import logger from './config/logger';
import "reflect-metadata";
import * as dotenv from 'dotenv';

dotenv.config();

import roomRouter from './routes/room.route';
import {connectToDb} from './config/typeorm.config';
const passport = PassportJs.initPassport();

async function main(): Promise<void> {

    logger.info('connecting to database...')
    await connectToDb()
    logger.info('connected to database')

    const app = express();
    app.use(bodyParser.json());
    app.use(passport.initialize());
    app.use(cors());

    app.use(roomRouter);

    app.listen(3000, () => {
        logger.info(`API is running on port ${3000}`);
    });
}
main().catch(error => {
    logger.error(error)
    process.exit(1)
})


Can you help me?你能帮助我吗?

Thank感谢

From the code snippets you have share, I guess const roomRepository = getRepository(Room) is being called before the await connectToDb() .从您共享的代码片段中,我猜const roomRepository = getRepository(Room)await connectToDb()之前被调用。 Creating a repo needs connection.创建 repo 需要连接。

暂无
暂无

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

相关问题 未使用 TypeORM 找到连接“默认” - Connection "default" was not found with TypeORM 尝试请求存储库时未使用 TypeORM 找到连接“默认” - Connection “default” was not found with TypeORM when trying to request a repository RepositoryNotFoundError:找不到“用户”的存储库。 看起来这个实体没有在当前的“默认”连接中注册? 打字机 - RepositoryNotFoundError: No repository for "User" was found. Looks like this entity is not registered in current "default" connection? Typeorm typeORM 未发现数据库架构更改 - 无法生成迁移。 要创建新的空迁移,请使用“typeorm migration:create”命令 - typeORM No changes in database schema were found - cannot generate a migration. To create a new empty migration use "typeorm migration:create" command TypeOrm-如何将连接用作具有类型的独立对象? - TypeOrm - How to use connection as standalone object with types? 无法在角度2中使用typeorm,未找到模块mysql - Failed to use typeorm in angular 2, module mysql not found 在连接池缓存中找不到 poolAlias “default”(将 Express 服务器连接到 Oracle 数据库) - poolAlias "default" not found in the connection pool cache (Connecting Express server to Oracle database) 找不到Express公共目录 - Express public directory cannot be found 打字稿表达打字稿创建连接 - typescript express typeorm createconnection TypeORM 关系和表达 - TypeORM relations and express
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM