在 TypeORM 中设置数据源会立即产生运行时错误

问题描述 投票:0回答:1

我无法让 Angular 与 TypeORM 一起使用。我几天前就提出了这个问题。同时,我已经找到了问题的原因。

现在我可以提供重现问题的最小设置。根据我的发现,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
下。这是全新 Angular 安装的标准行为,并显示欢迎屏幕。

接下来,我使用

安装了TypeORM
> npm install typeorm

added 37 packages, and audited 947 packages in 15s

122 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities

接下来,我创建了目录

src/app/control
并发出了 Angular 命令来生成数据源服务:

> ng generate service control/app.datasource
CREATE src/app/control/app.datasource.service.spec.ts (409 bytes)
CREATE src/app/control/app.datasource.service.ts (151 bytes)
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

虽然

app.datasource
服务尚未在任何其他组件中提及,但这会立即产生运行时错误:

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)

对我来说,这不是我对 TypeORM 模块的期望。


环境

Windows 10 (19045.4046) 上的 IDE Jetbrains WebStorm,AMD FX(tm)-8300 八核处理器 3.30 GHz

package.json

{
  "name": "typeorm-problem",
  "version": "0.0.0",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "watch": "ng build --watch --configuration development",
    "test": "ng test"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "^17.2.0",
    "@angular/common": "^17.2.0",
    "@angular/compiler": "^17.2.0",
    "@angular/core": "^17.2.0",
    "@angular/forms": "^17.2.0",
    "@angular/platform-browser": "^17.2.0",
    "@angular/platform-browser-dynamic": "^17.2.0",
    "@angular/router": "^17.2.0",
    "rxjs": "~7.8.0",
    "tslib": "^2.3.0",
    "typeorm": "^0.3.20",
    "zone.js": "~0.14.3"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "^17.2.1",
    "@angular/cli": "^17.2.1",
    "@angular/compiler-cli": "^17.2.0",
    "@types/jasmine": "~5.1.0",
    "jasmine-core": "~5.1.0",
    "karma": "~6.4.0",
    "karma-chrome-launcher": "~3.2.0",
    "karma-coverage": "~2.2.0",
    "karma-jasmine": "~5.1.0",
    "karma-jasmine-html-reporter": "~2.1.0",
    "typescript": "~5.3.2"
  }
}

tsconfig.json

/* To learn more about this file see: https://angular.io/config/tsconfig. */
{
  "compileOnSave": false,
  "compilerOptions": {
    "outDir": "./dist/out-tsc",
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "noImplicitOverride": true,
    "noPropertyAccessFromIndexSignature": true,
    "noImplicitReturns": true,
    "noFallthroughCasesInSwitch": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "sourceMap": true,
    "declaration": false,
    "experimentalDecorators": true,
    "moduleResolution": "node",
    "importHelpers": true,
    "target": "ES2022",
    "module": "ES2022",
    "useDefineForClassFields": false,
    "lib": [
      "ES2022",
      "dom"
    ]
  },
  "angularCompilerOptions": {
    "enableI18nLegacyMessageIdFormat": false,
    "strictInjectionParameters": true,
    "strictInputAccessModifiers": true,
    "strictTemplates": true
  }
}

angular.json

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "typeorm-problem": {
      "projectType": "application",
      "schematics": {},
      "root": "",
      "sourceRoot": "src",
      "prefix": "app",
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:application",
          "options": {
            "outputPath": "dist/typeorm-problem",
            "index": "src/index.html",
            "browser": "src/main.ts",
            "polyfills": [
              "zone.js"
            ],
            "tsConfig": "tsconfig.app.json",
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ],
            "styles": [
              "src/styles.css"
            ],
            "scripts": []
          },
          "configurations": {
            "production": {
              "budgets": [
                {
                  "type": "initial",
                  "maximumWarning": "500kb",
                  "maximumError": "1mb"
                },
                {
                  "type": "anyComponentStyle",
                  "maximumWarning": "2kb",
                  "maximumError": "4kb"
                }
              ],
              "outputHashing": "all"
            },
            "development": {
              "optimization": false,
              "extractLicenses": false,
              "sourceMap": true
            }
          },
          "defaultConfiguration": "production"
        },
        "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "configurations": {
            "production": {
              "buildTarget": "typeorm-problem:build:production"
            },
            "development": {
              "buildTarget": "typeorm-problem:build:development"
            }
          },
          "defaultConfiguration": "development"
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "buildTarget": "typeorm-problem:build"
          }
        },
        "test": {
          "builder": "@angular-devkit/build-angular:karma",
          "options": {
            "polyfills": [
              "zone.js",
              "zone.js/testing"
            ],
            "tsConfig": "tsconfig.spec.json",
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ],
            "styles": [
              "src/styles.css"
            ],
            "scripts": []
          }
        }
      }
    }
  }
}

问题

我做错了什么?我该如何使用标准 Angular 设置使 TypeORM 在浏览器中工作?

angular browser typeorm
1个回答
0
投票

作为代码的母猪,你没有安装 MariaDB 驱动程序。

适用于 MySQL 或 MariaDB npm install mysql --save (你也可以安装 mysql2)

© www.soinside.com 2019 - 2024. All rights reserved.