API 适用于打字稿,但当我转译时它找不到模型



我正在编写一个简单的API,用于在postgres数据库中创建、读取和删除学生信息。

当我使用ts-node-dev而没有将文件转换为javascript时,应用程序就可以工作了。但是,当我使用babel进行transfile并尝试运行node dist/server.js时,会出现以下错误:

(node:12771) UnhandledPromiseRejectionWarning: /Users/Wblech/Desktop/42_vaga/src/model/Student.ts:1
import { Entity, Column, PrimaryGeneratedColumn } from "typeorm";
^
SyntaxError: Unexpected token {
at Module._compile (internal/modules/cjs/loader.js:718:23)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:785:10)
at Module.load (internal/modules/cjs/loader.js:641:32)
at Function.Module._load (internal/modules/cjs/loader.js:556:12)
at Module.require (internal/modules/cjs/loader.js:681:19)
at require (internal/modules/cjs/helpers.js:16:16)
at Function.PlatformTools.load (/Users/Wblech/Desktop/42_vaga/node_modules/typeorm/platform/PlatformTools.js:114:28)
at /Users/Wblech/Desktop/42_vaga/node_modules/typeorm/util/DirectoryExportedClassesLoader.js:39:69
at Array.map (<anonymous>)
at Object.importClassesFromDirectories (/Users/Wblech/Desktop/42_vaga/node_modules/typeorm/util/DirectoryExportedClassesLoader.js:39:10)
(node:12771) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:12771) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

应用程序运行,但如果我使用任何http方法,它会返回以下错误:

{
"error": "No metadata for "Student" was found."
}

为了清楚起见,第一个错误是在终端中,在我执行命令";node-dist/server.js";。第二个错误是当我尝试http方法时,服务器运行,但找不到我的模型Student。

Student.ts是这样写的:

import { Entity, Column, PrimaryGeneratedColumn } from "typeorm";
@Entity("students")
class Student {
@PrimaryGeneratedColumn("increment")
id: number;
@Column()
name: string;
@Column()
intra_id: string;
@Column("simple-array")
projects: string;
}
export default Student;

我的ormconfig.ts是这样的:

{
"type": "postgres",
"host": "localhost",
"port": 5432,
"username": "postgres",
"password": "docker",
"database": "42sp",
"entities": [
"./src/model/*.ts"
],
"migrations": [
"./src/database/migrations/*.ts"
],
"cli": {
"migrationsDir": "./src/database/migrations"
}
}

我的包.json是这样的:

{
"name": "42_vaga",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"scripts": {
"build": "babel src --extensions ".js,.ts" --out-dir dist --copy-files",
"dev:server": "ts-node-dev --inspect --transpile-only --ignore-watch node_modules src/server.ts",
"typeorm": "ts-node-dev ./node_modules/typeorm/cli.js"
},
"devDependencies": {
"@babel/cli": "^7.10.5",
"@babel/core": "^7.10.5",
"@babel/node": "^7.10.5",
"@babel/plugin-proposal-class-properties": "^7.10.4",
"@babel/plugin-proposal-decorators": "^7.10.5",
"@babel/preset-env": "^7.10.4",
"@babel/preset-typescript": "^7.10.4",
"@types/express": "^4.17.7",
"babel-plugin-module-resolver": "^4.0.0",
"babel-plugin-transform-decorators": "^6.24.1",
"babel-plugin-transform-typescript-metadata": "^0.3.0",
"ts-node-dev": "^1.0.0-pre.51",
"typescript": "^3.9.6"
},
"dependencies": {
"@types/node": "^14.0.23",
"express": "^4.17.1",
"pg": "^8.3.0",
"reflect-metadata": "^0.1.13",
"swagger-ui-express": "^4.1.4",
"typeorm": "^0.2.25",
"uuidv4": "^6.1.1"
}
}

我的babel配置:

module.exports = {
presets: [
['@babel/preset-env', { targets: { node: 'current'}}],
'@babel/preset-typescript'
],
plugins : [
"babel-plugin-transform-typescript-metadata",
["@babel/plugin-proposal-decorators", { "legacy": true}],
["@babel/plugin-proposal-class-properties", { "loose": true}],
]
}

我的树文件夹是这样的:

├── README.md
├── babel.config.js
├── dist
│   ├── database
│   │   ├── index.js
│   │   └── migrations
│   │       └── 1594744103410-CreateStudents.js
│   ├── model
│   │   └── Student.js
│   ├── repositories
│   │   └── StudentRepository.js
│   ├── routes
│   │   ├── index.js
│   │   └── students.routes.js
│   ├── server.js
│   └── services
│       └── CreateStudentService.js
├── documentation.md
├── instructions.md
├── ormconfig.json
├── package.json
├── src
│   ├── database
│   │   ├── index.ts
│   │   └── migrations
│   │       └── 1594744103410-CreateStudents.ts
│   ├── model
│   │   └── Student.ts
│   ├── repositories
│   │   └── StudentRepository.ts
│   ├── routes
│   │   ├── index.ts
│   │   └── students.routes.ts
│   ├── server.ts
│   └── services
│       └── CreateStudentService.ts
├── tsconfig.json
├── yarn-error.log
└── yarn.lock

我不明白为什么当是typescript时它确实有效,但当是javascript时它就不起作用了。

问题出现在ormConfig.ts 中

`{"类型":"postgres";,

"host": "localhost",
"port": 5432,
"username": "postgres",
"password": "docker",
"database": "42sp",
"entities": [
"./src/model/*.ts" // so here you specify the ts file but after transpile 
//  it will convert into js so you need to do some 
// correction here so that it can work in ts and js mode too
],
"migrations": [
"./src/database/migrations/*.ts"
],
"cli": {
"migrationsDir": "./src/database/migrations"
}

}`

只需更改以下代码行的实体:-

entities: [__dirname + "/../entities/*.entity{.ts,.js}"],

所以现在它也将在transpile之后工作。

希望它能帮助

最新更新