在 express js 中引导后获取连接 [英] Get connection after bootstraping in express js

查看:26
本文介绍了在 express js 中引导后获取连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将 TypeORM 与 expressjs 一起使用,但在引导后无法连接.

I use TypeORM with expressjs but I am unable to the connection after bootstrapping it.

在我的 app.js 中,我有

import 'reflect-metadata';
import { createConnection, ConnectionOptions } from 'typeorm';
// Other imports

const app: Application = express();

// Setup express-async-errors
asyncHandler;

createConnection({
  "type": "sqlite",
  "database": "database.sqlite",
  "synchronize": true,
  "logging": true,
  "entities": [
    path.join(__dirname, "app/entity/**/*.js")
  ],
}).then(async connection => {

  // Set Environment & middleware
  middleware(app);

  // setup routes
  routes(app);

  app.listen(3000);
}).catch(error => console.log(error));

 export default app;

然后,我有一个 UsersController.ts 链接到用户路由

Then, I have a UsersController.ts which is linked to a the user routes

import { Request, Response } from 'express';
import { User } from '../entity/User';
import { getConnection } from "typeorm";

class UsersController {
  private userRepository;

  constructor() {
    this.userRepository = getConnection().getRepository(User);
  }

  async index(req: Request, res: Response) {
    const users = await this.userRepository.find();

    res.json({
      users
    });
  }
}

export default UsersController;

然而,如果我尝试运行上面的代码,我总是得到

However, if I try to run the above code, I always get

ConnectionNotFoundError:未找到连接默认"..

[ 'ConnectionNotFoundError:未找到连接默认".',' 在新的 ConnectionNotFoundError (C:[user]\node_modules\typeorm\error\ConnectionNotFoundError.js:19:28)',' 在 ConnectionManager.get (C:[user]\node_modules\typeorm\connection\ConnectionManager.js:38:19)',' 在 Object.getConnection (C:[user]\node_modules\typeorm\index.js:268:35)',' 在新的 UsersController (C:[user]\build\app\controllers\users.controller.js:7:41)',' 在对象.(C:[用户]\build\app\routes\users.route.js:12:19)',' 在 Module._compile (internal/modules/cjs/loader.js:689:30)',' 在 Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)',' 在 Module.load (internal/modules/cjs/loader.js:599:32)',' 在 tryModuleLoad (internal/modules/cjs/loader.js:538:12)',' 在 Function.Module._load (internal/modules/cjs/loader.js:530:3)' ] }

[ 'ConnectionNotFoundError: Connection "default" was not found.', ' at new ConnectionNotFoundError (C:[user]\node_modules\typeorm\error\ConnectionNotFoundError.js:19:28)', ' at ConnectionManager.get (C:[user]\node_modules\typeorm\connection\ConnectionManager.js:38:19)', ' at Object.getConnection (C:[user]\node_modules\typeorm\index.js:268:35)', ' at new UsersController (C:[user]\build\app\controllers\users.controller.js:7:41)', ' at Object. (C:[user]\build\app\routes\users.route.js:12:19)', ' at Module._compile (internal/modules/cjs/loader.js:689:30)', ' at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)', ' at Module.load (internal/modules/cjs/loader.js:599:32)', ' at tryModuleLoad (internal/modules/cjs/loader.js:538:12)', ' at Function.Module._load (internal/modules/cjs/loader.js:530:3)' ] }

我查看了 typeORM 在线文档,上面是推荐的设置 TypeORM 的方法,所以我很困惑.

I have checked the typeORM online documentation and what I have above is the recommended way to setup TypeORM so, I am confused.

任何指向正确方向的指针将不胜感激.

Any pointer, in the right direction will be appreciated.

推荐答案

出现错误是因为初始化代码TypeORM"异步运行,其余代码向前运行.解决方法是需要在块之后注册所有的express路由(createConnection({}).then).该文档有一个使用 express 的示例:

The error occurs because the initialization code "TypeORM" runs asynchronously and the rest of the code runs forward. The solution is that you need to register all express routing in the block after then (createConnection ({}).then ). The documentation has an example of working with express:

http://typeorm.io/#/example-with-express

这篇关于在 express js 中引导后获取连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆