从typescript命名空间导出类在编译后给出undefined(NestJS)



问题很简单,我想使用名称空间来重新组合一些类,但每当我试图从该名称空间导入类时,它都会返回

无法读取未定义命名空间错误的属性。

下面是一个示例

命名空间文件示例:

import { Entity, Column, BaseEntity, Connection, QueryRunner, MigrationInterface, Table } from "typeorm";
import { DB_CONNECTION_PROVIDER } from "./database.providers";
import { EntityDefinition } from "./entity.definition";
/**
* Each namespace has to have Entity and Migration functions
* @definition define the user repository, service, table and schema name the table schema
* @entity define the table schema
* @TableMigration define the table migration(up, down)
*/ 
export namespace User{
export enum ROLE {
SUPERADMIN = "SUPERADMIN",
CLIENT = "CLIENT",
}
export class Definition implements EntityDefinition {
static readonly tableName = "users";
static readonly schemaName = "user";
static readonly REPOSITORY_NAME = "USERS_REPOSITORY";
static service = {
provide: Definition.REPOSITORY_NAME,
useFactory: (connection: Connection) => connection.getRepository(UEntity),
inject: [DB_CONNECTION_PROVIDER],
};
}
export class UTable implements MigrationInterface {
constructor() {
}
public async up(queryRunner: QueryRunner): Promise<any> {
await queryRunner.createTable(new Table(UEntity), false,);
}
public async down(queryRunner: QueryRunner): Promise<any> {
// queryRunner.query(`DROP TABLE article`);
}
}

@Entity({ name: User.Definition.tableName, schema: User.Definition.schemaName })
export class UEntity extends BaseEntity {
@Column({ type: "string", nullable: false })
firstname: string;
@Column({ type: "string", nullable: false })
lastname: string;
@Column({ type: "enum", enum: User.ROLE, nullable: false })
role: User.ROLE;
@Column({ type: "string", unique: true, nullable: false })
email: string;
@Column({ type: "string", nullable: false })
password: string;
@Column({ type: "string", nullable: false })
picture: string;
@Column({ type: "date", nullable: false })
birthday: Date;
@Column({ type: "string", nullable: false })
country: string;
@Column({ type: "string", nullable: false })
city: string;
@Column({ type: "string", nullable: false })
phone: string;
@Column({ type: "boolean", default: false })
isdelete: boolean;
@Column({ type: "string", nullable: true })
oauth_provider_id?: string;
@Column({ type: "string", nullable: true })
oauth_provider_token?: string;
@Column({ type: "timestamp" })
createdAt: Date;
@Column({ type: "timestamp" })
updatedAt: Date;
}
}

下面是我如何导入和使用名称空间类

import { User } from './user.entity';
import { createConnection } from 'typeorm';
import { PostgresConnectionOptions } from 'typeorm/driver/postgres/PostgresConnectionOptions';
const host = process.env.DB_HOST;
const port = parseInt(process.env.DB_PORT);
const username = process.env.DB_USERNAME;
const password = process.env.DB_PASSWORD;
const dbName = process.env.DB_NAME;
const migrations = [
User.UTable
]
const entities = [
User.UEntity
]
export const DB_CONNECTION_PROVIDER = "DATABASE_CONNECTION";
export const dbConfig: PostgresConnectionOptions = {
type: 'postgres',
host: host,
port: port,
username: username,
password: password,
database: dbName,
synchronize: false, // don't allow entity to update database alone
entities: [...entities],
migrations:[...migrations],
uuidExtension: "uuid-ossp",
}
export const databaseProvider = {
provide: DB_CONNECTION_PROVIDER,
useFactory: async () => await createConnection(dbConfig)
}

这是我得到的错误

user_entity_1.user.UTable

TypeError:无法读取未定义的属性"UTable"在对象处。(/Volumes/Secondary/Transport/Transport-app/dist/models/database.providers.js:12:24(在模块中_compile(internal/modules/cjs/loader.js:1063:30(位于Object.Module_extensions.js(internal/modules/cjs/loader.js:1092:10(在Module.load(internal/modules/cjs/loader.js:928:32(位于Function.Module_load(internal/modules/cjs/loader.js:769:14(在Module.require(internal/modules/cjs/loader.js:952:19(at required(internal/modules/cjs/helpers.js:88:18(在对象处。(/Volumes/Secondary/Transport/Transport-app/dist/models/user.entity.js:14:30(在模块中_compile(internal/modules/cjs/loader.js:1063:30(在Object.Module_extensions.js(internal/modules/cjs/loader.js:1092:10(

我不知道typescript或命名空间的问题是用例。

我希望你能向我解释发生了什么

好的,在这两个文件之间有一个循环引用。您的user.entity.ts导入database.provider.ts以获得DB_CONNECTION_PROVIDER令牌,database.provider.ts导入user.entity.ts以获取User命名空间。我在这里要做的是将DB_CONNECTION_PROVIDER令牌移动到一个单独的database.constants.ts文件中,并从两个文件中导入它,这样就可以删除文件之间的循环依赖关系

最新更新