【问题标题】:Typeorm Database not starting?Typeorm 数据库没有启动?
【发布时间】:2021-04-11 23:03:36
【问题描述】:

我使用选项 init typeorm --database postgres --express 引导 typeorm。

我更改了 ormconfig 以匹配我在数据库中的信息。

没有错误。但似乎什么都没有运行。

看来我的数据库没有问题,因为它没有抛出错误。

我缺少什么吗?

以下是默认设置


// index.ts 

import 'reflect-metadata';
import { createConnection } from 'typeorm';
import * as express from 'express';
import * as bodyParser from 'body-parser';
import { Request, Response } from 'express';
import { Routes } from './routes';
import { User } from './entity/User';

createConnection()
    .then(async (connection) => {
        // create express app
        const app = express();
        app.use(bodyParser.json());

        // register express routes from defined application routes
        Routes.forEach((route) => {
            (app as any)[route.method](
                route.route,
                (req: Request, res: Response, next: Function) => {
                    const result = new (route.controller as any)()[route.action](
                        req,
                        res,
                        next
                    );
                    if (result instanceof Promise) {
                        result.then((result) =>
                            result !== null && result !== undefined
                                ? res.send(result)
                                : undefined
                        );
                    } else if (result !== null && result !== undefined) {
                        res.json(result);
                    }
                }
            );
        });

        // setup express app here
        // ...

        // start express server
        app.listen(8080);

        // insert new users for test
        await connection.manager.save(
            connection.manager.create(User, {
                firstName: 'Timber',
                lastName: 'Saw',
                age: 27,
            })
        );
        await connection.manager.save(
            connection.manager.create(User, {
                firstName: 'Phantom',
                lastName: 'Assassin',
                age: 24,
            })
        );

        console.log(
            'Express server has started on port 3000. Open http://localhost:3000/users to see results'
        );
    })
    .catch((error) => console.log(error));

【问题讨论】:

  • Paul,您能否详细说明您的陈述“似乎什么都没有运行”?你的意思是你的console.log() 调用永远不会产生输出?

标签: node.js express typeorm


【解决方案1】:

我认为您没有配置数据库详细信息。您必须将 ormconfig 文件添加到您的项目中。

第 1 步:- 在根目录中创建一个名为 ormconfig.ts 的文件。

第 2 步:- 将以下代码添加到该文件中,并将您的数据库详细信息放在这里。

{
  "type": "postgres", 
  "host": "localhost", 
  "port": 3306,
  "username": "root",
  "password": "",
  "database": "test",
  "synchronize": true,
  "entities": ["here put paths for your entities"],
}

【讨论】:

    【解决方案2】:

    首先你必须使用 ormconfig 文件来配置你的 typeorm 配置。

    【讨论】:

      猜你喜欢
      • 2020-03-25
      • 1970-01-01
      • 1970-01-01
      • 2019-06-17
      • 2019-05-09
      • 1970-01-01
      • 2018-12-14
      • 2019-07-10
      • 2021-04-29
      相关资源
      最近更新 更多