node.js - 编译后从 typescript 命名空间导出类给出未定义(NestJS)

标签 node.js typescript class namespaces nestjs

问题很简单,我想使用命名空间来重新组合一些类,但是每当我尝试从这个命名空间导入一个类时,它就会返回

cannot read property of undefined namespace error.


这是一个例子
命名空间文件示例:
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: Cannot read property 'UTable' of undefined at Object. (/Volumes/Secondary/Transport/transport-app/dist/models/database.providers.js:12:24) at Module._compile (internal/modules/cjs/loader.js:1063:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10) at Module.load (internal/modules/cjs/loader.js:928:32) at Function.Module._load (internal/modules/cjs/loader.js:769:14) at Module.require (internal/modules/cjs/loader.js:952:19) at require (internal/modules/cjs/helpers.js:88:18) at Object. (/Volumes/Secondary/Transport/transport-app/dist/models/user.entity.js:14:30) at Module._compile (internal/modules/cjs/loader.js:1063:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)


我不知道是 typescript 还是命名空间用例的问题。
我希望你能向我解释发生了什么

最佳答案

好的,您在两个文件之间有一个循环引用。您的 user.entity.ts进口database.provider.ts获取 DB_CONNECTION_PROVIDER token ,以及 database.provider.ts进口user.entity.ts获取 User命名空间。我在这里要做的是移动 DB_CONNECTION_PROVIDER token 到单独的 database.constants.ts文件,然后在两个文件中从那里导入它,这样你就可以删除文件之间的循环依赖

关于node.js - 编译后从 typescript 命名空间导出类给出未定义(NestJS),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68184057/

相关文章:

json - 如何访问 xml2js 结果属性

javascript - 删除 npm 包时会发生什么?

html - 使用内联 css - 在某些情况下是不行还是可以?

python - 使用 ADT 方法转换程序 python

http - node.js - 可能的 http 服务器内存泄漏

javascript - 如何将实时文件更新从 Node js服务器(express)发送到 Angular js

node.js - 如何在 NodeJS 中使用只有 URL 的 Sharp 调整图像大小,使用 async/await,并且没有创建本地副本?

reactjs - Material-UI 版本 5 中的主题类型是什么?

typescript - 如何实现具有多个调用签名的函数?

c# - 类型转换类和设置通用值