Angular集成TypeORM配置DataSource触发即时运行时错误求助
Angular与TypeORM集成运行时错误排查
无法让Angular与TypeORM协同工作,已提供最小复现环境。
复现步骤
- 创建全新Angular项目:
"C:\Program Files\nodejs\npx.cmd" --yes --package @angular/cli@latest ng new typeorm-problem --defaults --standalone=true
项目可通过ng serve在http://localhost:4200正常运行。
- 安装TypeORM:
npm install typeorm
安装过程无漏洞。
- 创建
src/app/control目录并生成数据源服务:
ng generate service control/app.datasource
服务代码如下:
import { Injectable } from '@angular/core'; import {DataSource} from "typeorm"; @Injectable({ providedIn: 'root' }) export class AppDatasourceService { constructor() { } } export const AppDataSource = new DataSource({ type: "mariadb", port: 3306, database: "test-db", host: "test-host", username: "test-user", password: "test-password", entities: ["../model/entities/*.{ts,js}",], synchronize: true, logging: true, // set to false on production });
- 运行
ng test时,即使未在任何组件中引用该服务,仍触发即时运行时错误。
运行时错误
Uncaught TypeError: Cannot read properties of undefined (reading 'type') TypeError: Cannot read properties of undefined (reading 'type') at EntityManagerFactory.create (http://localhost:9876/_karma_webpack_/webpack:/node_modules/typeorm/browser/entity-manager/EntityManagerFactory.js:12:39) at DataSource.createEntityManager (http://localhost:9876/_karma_webpack_/webpack:/node_modules/typeorm/browser/data-source/DataSource.js:403:43) at new DataSource (http://localhost:9876/_karma_webpack_/webpack:/node_modules/typeorm/browser/data-source/DataSource.js:54:29) at Module.7752 (http://localhost:9876/_karma_webpack_/webpack:/src/app/control/app.datasource.service.ts:11:30) at __webpack_require__ (http://localhost:9876/_karma_webpack_/webpack:/webpack/bootstrap:19:1) at Module.4004 (http://localhost:9876/_karma_webpack_/main.js:90:81) at __webpack_require__ (http://localhost:9876/_karma_webpack_/webpack:/webpack/bootstrap:19:1) at __webpack_exec__ (http://localhost:9876/_karma_webpack_/main.js:198:48) at http://localhost:9876/_karma_webpack_/main.js:199:126 at Function.__webpack_require__.O (http://localhost:9876/_karma_webpack_/webpack:/webpack/runtime/chunk loaded:23:1)
环境信息
- IDE:Jetbrains WebStorm(Windows 10)
- 处理器:AMD FX(tm)-8300 Eight-Core Processor 3.30 GHz
- 相关配置文件(package.json、tsconfig.json、angular.json)见原文
疑问
哪里操作有误?如何让TypeORM在标准Angular浏览器环境中正常工作?
内容的提问来源于stack exchange,提问作者hww
相关产品推荐
相关产品推荐

