- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
问题很简单,我想使用命名空间来重新组合一些类,但是每当我尝试从这个命名空间导入一个类时,它就会返回
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)
最佳答案
好的,您在两个文件之间有一个循环引用。您的 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/
我在开发应用程序时将配置保存在 .env 文件中。 这是我的 app.module.ts: @Module({ imports: [ ConfigModule.forRoot({ isGl
我正在使用我购买的 akveo 后端包,虽然在开发模式下一切似乎都在生产中运行良好,但出现了以下错误,但我对 nestjs 本身并不熟悉。 有谁知道这里发生了什么? node_modules/@nes
我刚刚开始使用 Nestjs我想知道如何使用路由前缀或通过 Express Router 实例对我的 API 进行版本控制? 理想情况下,我希望通过以下方式访问端点: /v1 /v2 等等,这样我就可
我想了解将服务提供商注入(inject) NestJS Controller 的目的是什么?这里的文档在这里解释了如何使用它们,这不是这里的问题:https://docs.nestjs.com/pro
我正在使用@goevelup/nestjs-rabbitmq库构建一个NestJS应用程序,以便将消息发布到rabbitmq交易所。。我正在AppModule中导入和配置RabbitMQ模块(这部分似
我正在使用@goevelup/nestjs-rabbitmq库构建一个NestJS应用程序,以便将消息发布到rabbitmq交易所。。我正在AppModule中导入和配置RabbitMQ模块(这部分似
我正在制作 @nestjs/swagger生成api文档。但是如何为经过身份验证的路由生成文档? 嵌套版本 λ nest i NodeJS Version : v10.16.0 [Nest Infor
NestJs 允许导出模块和提供者。它们有什么区别? 例子: // Reusable module @Module({ providers: [ServiceA], exports: [Service
我有一个返回字符串的 Controller 处理程序。 // Controller.ts import { Controller, Get, UseInterceptors } from '@nest
在 NestJS API 上,我想在实体中使用模块服务,以使用非数据库属性填充该实体。 在我的例子中,我想获得我正在检索的类别的文章数量。 @Entity({ name: 'categories' }
我正在设置一个新的 NestJS 应用程序,我刚刚添加了类验证器以验证 Controller 输入,但它似乎被完全忽略了。这是 DTO: import {IsString} from 'class-v
我想要一些环境,比如说development , production , test .这些环境应该是独立的,并使用它们自己的配置参数集,例如对于 DB、SERVER_PORT、USER 等。 它们不
我觉得 this thread 和 this thread 的组合是我需要实现的,我无法将它们绘制在一起。 我有一个包含 enum 的 DTO。 使用 Postman,我发送 PurchasableT
我是 NestJs 的新手,我创建了一个回退异常过滤器,现在我想知道如何使用它。换言之,如何将其导入我的应用程序? 这是我的后备异常过滤器: @Catch(HttpException) export
我在oracle数据库中有存储过程,我想在NestJs中调用它。 如何在 NestJs 中调用存储过程? 这是我的存储过程 PROCEDURE pipeline_critical (
我想在 nestjs 的验证中使用正则表达式。 例如: 正则表达式 pagePattern = '[a-z0-9\-]+'; 方法 @Get('/:article') getIndex(
我想将配置字符串传递给管道,但也想注入(inject)服务。 NesJs 文档描述了如何相互独立而不是一起执行这两项操作。举个例子: 管道.ts @Injectable() export class
我正在尝试对具有来自 nestjs 护照模块的 AuthGuard 的路由进行端到端测试,但我真的不知道如何处理它。当我运行测试时,它说: [ExceptionHandler] Unknown aut
我正在编写一个 NestJS 应用程序。一些端点支持排序,例如http://127.0.0.1:3000/api/v1/members?sort=-id&take=100 这意味着按 id 降序排序。
我想在 nestjs 的验证中使用正则表达式。 例如: 正则表达式 pagePattern = '[a-z0-9\-]+'; 方法 @Get('/:article') getIndex(
我是一名优秀的程序员,十分优秀!