gpt4 book ai didi

node.js - 实体元数据未找到 : Node. js TypeScript TypeORM

转载 作者:行者123 更新时间:2023-12-05 07:29:32 30 4
gpt4 key购买 nike

我是 typescript + typeorm 的新手,很长一段时间以来一直陷入以下问题。我在 github 上阅读了很多问题,但找不到问题。

我的项目结构

<projectname>
- src
- entity
- patient.ts
- migration
- app.ts
- server.ts

- package.json
- ormconfig.json
...

package.json

{
"name": "fir-server",
"version": "0.0.0",
"private": true,
"main": "app.js",
"scripts": {
"build": "tsc",
"dev": "ts-node ./src/server.ts",
"start": "nodemon ./dist/src/server.js",
"test": "mocha --recursive './test/*Test.js' --compilers js:babel-core/register"
},
"dependencies": {
"babel-core": "^6.25.0",
"babel-polyfill": "^6.23.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-stage-0": "^6.24.1",
"babel-register": "^6.26.0",
"express": "^4.15.4",
"mysql2": "^1.6.1",
"reflect-metadata": "^0.1.10",
"typeorm": "0.2.7",
...
},
"devDependencies": {
"@types/node": "^8.0.29",
"ts-node": "3.3.0",
"typescript": "2.5.2",
....
}
}

ormconfig.json

{
"type": "mysql",
"host": "localhost",
"port": 3306,
"username": "db_user",
"password": "db_password",
"database": "firdb",
"synchronize": false,
"migrationsRun": true,
"logging": false,
"entities": [
"dist/src/entities/*.js"
// tried this as well:
// src/entities/*.js
// src/entities/**/**.js, src/entities/*.ts, src/entities/**/**.ts
],
"migrations": [
"dist/src/migration/*.js"
],
"subscribers": [
"dist/src/subscriber/*.js"
],
"cli": {
"entitiesDir": "src/entity",
"migrationsDir": "src/migration",
"subscribersDir": "src/subscriber"
}

源/应用程序.ts

import "reflect-metadata";
import * as express from "express";
import * as bodyParser from "body-parser";

import { Routes } from "../routes/index";

class App {
public app: express.Application;
public route: Routes = new Routes();

constructor() {
this.app = express();
this.config();
this.route.routes(this.app);
}

private config(): void {
// support application/json type post data
this.app.use(bodyParser.json());
//support application/x-www-form-urlencoded post data
this.app.use(bodyParser.urlencoded({ extended: false }));
}

}

export default new App().app;

src/server.ts

import app from "./app"
import { createConnection } from 'typeorm';

const port = 3000

createConnection().then(async (connection) => {
app.listen(port, () => {
console.log('Express server listening on port ' + port);
})
}).catch((error) => console.log(error));

患者.ts

import { Entity, PrimaryGeneratedColumn, Column } from "typeorm";

@Entity()
export class Patient {

@PrimaryGeneratedColumn()
id: number;

@Column({ name: "first_name" })
firstName: string;

@Column({ name: "middle_name" })
middleName: string;

@Column({ name: "last_name" })
lastName: string;

@Column()
username: string;

@Column()
gender: string;

@Column()
phone_number: string;

@Column()
email: string;

@Column()
is_mother: boolean;
}

运行命令后:

tsc

一个新的 dist 目录在项目根目录下创建,结构如下:

- dist
- src
- entity
- patient.js
- patient.js.map

- migration

我在我的 Controller 中这样使用它:

import { Patient } from 'src/entity/patient'

let patients = getConnection().manager.find(Patient);

我得到的错误:

UnhandledPromiseRejectionWarning: EntityMetadataNotFound: No metadata for "Patient" was found

最佳答案

我最近遇到了同样的问题。在网上搜索时,我发现之前有人遇到过类似的问题。因此,我将贡献我的解决方案,也许有人觉得它有帮助。

错误码是因为TypeORM找不到实体所在的目录。

首先,我们必须了解我们的文件是在构建后从 dist 文件夹提供的。因此,路径必须相对于 dist 文件夹。

在我的例子中,datasource.ts 在 src 文件夹中。这意味着在构建之后,即编译 ts 时,它将位于根 dist 文件夹 中。

我使用的解决方案:


const AppDataSource = new DataSource( {
type: 'mysql',
host: DATABASE_HOST,
port: DATABASE_PORT,
username: DATABASE_USERNAME,
password: DATABASE_PASSWORD,
database: DATABASE_NAME,
entities: [ `${ __dirname }/entity/*.entity.js`]
} );

注意:我命名了所有以 .entity.js 结尾的实体文件。如果您只使用 .js,它会工作正常。

如:


const AppDataSource = new DataSource( {
type: 'mysql',
host: DATABASE_HOST,
port: DATABASE_PORT,
username: DATABASE_USERNAME,
password: DATABASE_PASSWORD,
database: DATABASE_NAME,
entities: [ `${ __dirname }/entity/*.js` ]
} );

检查:[BUG] web/nestjs: EntityMetadataNotFoundError: No metadata was found.如果您仍然感到困惑。

关于node.js - 实体元数据未找到 : Node. js TypeScript TypeORM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52689751/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com