简体   繁体   English

在类中使用TypeORM找不到连接默认值

[英]Connection default not found using TypeORM in a class

I am trying to use TypeORM inside a class but for some reason it can not find the default connection, I am awaiting the connection and I am sure that the config is right because I tested it with .then() and that did work 我试图在类中使用TypeORM但由于某种原因它无法找到默认连接,我正在等待连接,我确信配置是正确的,因为我用.then()测试了它确实有效

class App {
    public app: express.Application;

    constructor() {
        this.app = express();
        this.connect();
        this.test();
        this.config();
        this.routes();
    }

    private async connect(): Promise<Connection> {
        return createConnection();
    }

    private async test(): Promise<User> {
        const repository = getRepository(User);
        const user = new User();
        user.firstName = 'Daniell';
        user.lastName = 'lastname';
        return repository.save(user);
    }

I call the class like 我称之为班级

import App from './App';
import { Server } from './Server';

(() => new Server(App))();

Why can it not find the default connection? 为什么找不到默认连接?

Fixed it by changing the class like 通过更改类来修复它

class App {
    public app: express.Application;
    private connection: Promise<Connection>;

    constructor() {
        this.connection = createConnection();
        this.config();
    }

    private async config(): Promise<void> {
        // Load TypeORM with ormconfig.json options
        await this.connection;

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM