gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 17:05:23 24 4
gpt4 key购买 nike

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

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 undefinedat 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/

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