【问题标题】:TypeORM No metadata for \"MyEntity\" was foundTypeORM 找不到 \\\"MyEntity\\\" 的元数据
【发布时间】:2022-10-06 03:42:13
【问题描述】:

我 1. 在 \"app-data-source.ts\" 上有以下数据源

import { DataSource } from \"typeorm\";
import { App } from \"./entities/app\";

export const appDataSource = new DataSource({
    type: \'postgres\',
    host: process.env.CONFIG_DB_HOST,
    port: 5432,
    username: process.env.CONFIG_DB_USER,
    password: process.env.CONFIG_DB_PASSWORD,
    database: process.env.CONFIG_DB_DATABASE,
    entities: [App],
    synchronize: false,
});

  1. 在“entities/app.ts”上使用实体App
import { Column, Entity, PrimaryColumn } from \"typeorm\";

@Entity(\'apps\')
export class App {
    @PrimaryColumn()
    tenant_id: number;
    
    @Column()
    client_id: string;
    
    @Column()
    legacy_client_id: string;

    @Column()
    user_pool: string;
}
  1. 以及以下查询实体的模块应用程序(如上图 2 所示)。
import { App } from \'./entities/app\';
import { appDataSource } from \'./app-data-source\';

export class AuthService {
   async getApp() {
     let tenant=  await appDataSource.getRepository(App).findOneBy({
                client_id: clientId
            });
     }
}

但是我收到以下错误。

{
    \"errorMessage\": \"No metadata for \\\"App\\\" was found.\",
    \"errorType\": \"EntityMetadataNotFoundError\",
    \"stackTrace\": [
        \"EntityMetadataNotFoundError: No metadata for \\\"App\\\" was found.\",
        \"    at DataSource.getMetadata (D:\\\\lami-accounts\\\\dist\\\\apps\\\\auth\\\\main.js:181364:19)\",
        \"    at get metadata [as metadata] (D:\\\\lami-accounts\\\\dist\\\\apps\\\\auth\\\\main.js:185119:40)\",
        \"    at Repository.findOneBy (D:\\\\lami-accounts\\\\dist\\\\apps\\\\auth\\\\main.js:185312:44)\",
        \"    at AuthService.getAccessToken (D:\\\\lami-accounts\\\\dist\\\\apps\\\\auth\\\\main.js:57451:89)\",
        \"    at handler (D:\\\\lami-accounts\\\\dist\\\\apps\\\\auth\\\\main.js:32:27)\"
    ]
}

    标签: postgresql nestjs datasource typeorm


    【解决方案1】:

    显然,在执行查询之前我没有通过调用appDataSource.initialize(); 函数来初始化连接。?‍♂️ 就像这样。

    // initialize
    await appDataSource.initialize();
    let tenant=  await appDataSource.getRepository(App).findOneBy({
        client_id: clientId
    }); 
    
    // destroy the connection
    await appDataSource.destroy()
    

    【讨论】:

      猜你喜欢
      • 2023-03-19
      • 2019-01-04
      • 1970-01-01
      • 2019-02-26
      • 2021-01-18
      • 1970-01-01
      • 1970-01-01
      • 2018-05-09
      • 2020-06-25
      相关资源
      最近更新 更多