API 适用于打字稿,但当我转译时它找不到模型
API works with typescript but when I transpile it doesn´t find the model
我正在编写一个简单的 API 来创建、读取和删除 postgres 数据库中的学生信息。
当我使用 ts-node-dev
而不将文件转换为 javascript 时,应用程序可以正常工作。但是当我使用 babel 转译并尝试 运行 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.
App 运行s,但是如果我执行任何 http 方法它 returns 这个错误:
{
"error": "No metadata for \"Student\" was found."
}
需要说明的是,第一个错误出现在终端中,在我执行命令“node dist/server.js”之后。第二个错误是当我尝试使用 http 方法时,服务器 运行s 但它没有找到我的模型 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"
}
}
我的package.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
我不明白为什么它在打字稿时有效,但在 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}"],
所以现在它也可以在转译后工作了。
希望对您有所帮助
我正在编写一个简单的 API 来创建、读取和删除 postgres 数据库中的学生信息。
当我使用 ts-node-dev
而不将文件转换为 javascript 时,应用程序可以正常工作。但是当我使用 babel 转译并尝试 运行 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.
App 运行s,但是如果我执行任何 http 方法它 returns 这个错误:
{
"error": "No metadata for \"Student\" was found."
}
需要说明的是,第一个错误出现在终端中,在我执行命令“node dist/server.js”之后。第二个错误是当我尝试使用 http 方法时,服务器 运行s 但它没有找到我的模型 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"
}
}
我的package.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
我不明白为什么它在打字稿时有效,但在 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}"],
所以现在它也可以在转译后工作了。
希望对您有所帮助