FE - Eggjs 结合 Typeorm 出现连接不了数据库

问题

eggjs 结合 typeorm 打包部署的时候,一致报错,刚开始还以为是 eggjs 的问题

[egg-scripts] Got error when startup: 
[egg-scripts]     at createScript (vm.js:251:10)
[egg-scripts]     at Object.runInThisContext (vm.js:303:10)
[egg-scripts]     at Module._compile (internal/modules/cjs/loader.js:656:28)
[egg-scripts]     at Object.Module._extensions..js (internal/modules/cjs/loader.js:699:10)
[egg-scripts]     at Module.load (internal/modules/cjs/loader.js:598:32)
[egg-scripts]     at tryModuleLoad (internal/modules/cjs/loader.js:537:12)
[egg-scripts]     at Function.Module._load (internal/modules/cjs/loader.js:529:3)
[egg-scripts]     at Module.require (internal/modules/cjs/loader.js:636:17)
[egg-scripts]     at require (internal/modules/cjs/helpers.js:20:18)
[egg-scripts] 
[egg-scripts] pid: 30800
[egg-scripts] hostname: labelnet
[egg-scripts] 
[egg-scripts] 2019-03-05 15:17:28,085 ERROR 30796 [-/127.0.0.1/-/1ms GET /] nodejs.SyntaxError: Unexpected token {
[egg-scripts] (function (exports, require, module, __filename, __dirname) { import {
[egg-scripts]                                                                      ^
[egg-scripts] 
[egg-scripts] SyntaxError: Unexpected token {
[egg-scripts]     at new Script (vm.js:79:7)
[egg-scripts]     at createScript (vm.js:251:10)
[egg-scripts]     at Object.runInThisContext (vm.js:303:10)
[egg-scripts]     at Module._compile (internal/modules/cjs/loader.js:656:28)
[egg-scripts]     at Object.Module._extensions..js (internal/modules/cjs/loader.js:699:10)
[egg-scripts]     at Module.load (internal/modules/cjs/loader.js:598:32)
[egg-scripts]     at tryModuleLoad (internal/modules/cjs/loader.js:537:12)
[egg-scripts]     at Function.Module._load (internal/modules/cjs/loader.js:529:3)
[egg-scripts]     at Module.require (internal/modules/cjs/loader.js:636:17)
[egg-scripts]     at require (internal/modules/cjs/helpers.js:20:18)

eggjs 的 issues 都看过类似的问题了 ,

https://github.com/eggjs/egg/issues/2388

但官网文档已经明确说明,先回加载 js 文件 后加载 ts 文件,一直以为是 eggjs 的问题;

运行 npm start 不会加载 ts

没有想到是 typeorm 的配置问题,entity 配置的是 ts 文件,生产环境需要配置 js 文件

typeorm 并不会自动加载 entity 的 js 文件

解决

开发环境 entity 加载 ts 文件 ,生产环境 加载 js 文件

   "db_dev": {
        "database": "ruisite-dash-server",
        "host": "127.0.0.1",
        "port": "3306",
        "username": "root",
        "password": "root",
        "logging": true,
        "entities": ["app/entity/*.ts", "app/entity/*/*.ts"]
    }
    
    "db_production": {
        "database": "ruisite-dash-server",
        "host": "192.168.100.10",
        "port": "3306",
        "username": "root",
        "password": "root",
        "logging": true,
        "entities": ["app/entity/*.js", "app/entity/*/*.js"]
    }
展开阅读全文
©️2020 CSDN 皮肤主题: 点我我会动 设计师: 上身试试 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值