TypeORM 找不到“MyEntity”的元数据

Jam*_*nga 2 postgresql datasource typeorm nestjs

我 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,
});

Run Code Online (Sandbox Code Playgroud)
  1. 它在“entities/app.ts”上使用实体应用程序
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;
}
Run Code Online (Sandbox Code Playgroud)
  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
            });
     }
}
Run Code Online (Sandbox Code Playgroud)

但是我收到以下错误。

{
    "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)"
    ]
}
Run Code Online (Sandbox Code Playgroud)

Jam*_*nga 5

appDataSource.initialize();显然,我在执行查询之前没有通过调用函数来初始化连接。 \xe2\x80\x8d\xe2\x99\x82\xef\xb8\x8f 像这样。

\n
// initialize\nawait appDataSource.initialize();\nlet tenant=  await appDataSource.getRepository(App).findOneBy({\n    client_id: clientId\n}); \n\n// destroy the connection\nawait appDataSource.destroy()\n
Run Code Online (Sandbox Code Playgroud)\n