在 express js 中引导后获取连接
Get connection after bootstraping in express js
我将 TypeORM 与 expressjs 一起使用,但在引导后无法连接。
在我的 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
链接到用户路由
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;
但是,如果我尝试 运行 上面的代码,我总是得到
ConnectionNotFoundError: Connection "default" was not found.
.
[ '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 的方法,所以我很困惑。
任何指向正确方向的指针将不胜感激。
出现错误是因为初始化代码"TypeORM"异步运行,其余代码向前运行。解决方法是需要在then(createConnection({}).then)之后的block中注册所有的express routing。该文档有一个使用 express 的示例:
我将 TypeORM 与 expressjs 一起使用,但在引导后无法连接。
在我的 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
链接到用户路由
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;
但是,如果我尝试 运行 上面的代码,我总是得到
ConnectionNotFoundError: Connection "default" was not found.
.[ '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 的方法,所以我很困惑。
任何指向正确方向的指针将不胜感激。
出现错误是因为初始化代码"TypeORM"异步运行,其余代码向前运行。解决方法是需要在then(createConnection({}).then)之后的block中注册所有的express routing。该文档有一个使用 express 的示例: