【问题标题】:Typeorm cannot connect to PostgresTypeorm 无法连接到 Postgres
【发布时间】:2021-04-18 15:40:13
【问题描述】:

我正在使用 Typeorm 连接到数据库,但它无法连接并且没有显示错误。 如果我评论建立连接的行,一切正常。

这是我的代码

index.ts


import {App} from './app';
import { createConnection,Connection } from "typeorm";
import * as dotenv from 'dotenv';
import config  from './ormconfig';

async function main() {    
    dotenv.config();   
    try{
        await createConnection(config);
       
        console.log('Connected to the database');
    }catch(error){
        console.log(`Error connecting to the database\n ${error}`.red);
    }
    const app = new App([
        // new GrandTotalController()
        // new AuthenticationController,
                        // new UserController(),
        //                 new AccountMonthController
                    ],
        5000);
    await app.listen();
}
main();

ormconfing.ts

import { ConnectionOptions } from 'typeorm';
import * as dotenv from 'dotenv';

dotenv.config();
const config: ConnectionOptions = {
  type: 'postgres',
  host: process.env.POSTGRES_HOST,
  username: process.env.POSTGRES_USER,
  password: process.env.POSTGRES_PASSWORD,
  database: process.env.POSTGRES_DB,
  port: Number(process.env.POSTGRES_PORT),
  entities: [
    __dirname + '/../**/*.entity{.ts,.js}',
  ],
  migrations: [
    'src/migrations/*.ts',
  ],
  cli: {
    migrationsDir: 'src/migrations',
  },
};
export = config;

我的终端:

【问题讨论】:

    标签: node.js typescript postgresql typeorm


    【解决方案1】:

    我发现解决这个问题的唯一方法是使用以下命令创建一个新项目:

    typeorm init --name MyProject --database postgres
    

    并使我的项目适应这种结构

    【讨论】:

      猜你喜欢
      • 2021-09-04
      • 2019-08-13
      • 2019-08-02
      • 2021-11-25
      • 2020-07-13
      • 1970-01-01
      • 2021-12-03
      • 1970-01-01
      • 2020-11-25
      相关资源
      最近更新 更多