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



我正在尝试在类中使用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);
    }

我把这门课称为

import App from './App';
import { Server } from './Server';
(() => new Server(App))();

为什么找不到默认连接?

通过更改类来修复它,例如

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;

最新更新