小编Jay*_*iel的帖子

如何在动态 Nestjs 模块中导入 registerAsync?

一直在使用 Advanced NestJS 研究动态模块:如何构建完全动态的 NestJS 模块。

据我所知,大多数人使用本指南来构建同步/异步动态模块。

但是我的问题是,如果我使用 registerAsync 方法,并且我的动态模块需要导入 HttpModule,而 HttpModule 的 register-options 是由我的动态模块提供的。

如何在动态模块中导入模块,动态模块提供选项?或者是处理这个问题的错误方法?如果是这样,你会如何构建它?


这是代码。这实际上是本教程的副本。正如您在 register 方法中看到的那样,它很简单 - 我只是传入选项。registerAsync 但是,我无法弄清楚该怎么做。


任何帮助深表感谢 :)

import { Module, DynamicModule, Provider, HttpModule } from "@nestjs/common";
import { InvoicesHealth } from "./invoices/invoices.health";
import { InvoicesResolver, InvoicesService } from "./invoices";
import {
  CustomerInvoicesOptions,
  CustomerInvoicesAsyncOptions,
  CustomerInvoicesOptionsFactory,
} from "./interfaces";
import { CUSTOMER_INVOICES_OPTIONS } from "./constants";
import { createCustomerInvoicesProviders } from "./providers/customer-invoices.providers";

@Module({
  imports: [],
  controllers: [],
  providers: [InvoicesHealth, InvoicesResolver, InvoicesService],
  exports: [InvoicesHealth],
})
export …
Run Code Online (Sandbox Code Playgroud)

import module dynamic typescript nestjs

7
推荐指数
2
解决办法
2100
查看次数

使用 class-validator 在 Nestjs 中添加所需参数的验证

使用类验证器,验证管道我想根据需要标记一些字段。我尝试使用@IsNotEmpty方法。当输入为空时,它会抛出 400 错误。但如果输入也丢失,我需要抛出错误。

DTO:地址对象,包含字段地址 1 和地址 2。我希望地址 1 为必需,地址 2 为可选

import {IsString, IsInt, IsNotEmpty } from 'class-validator';
import {ApiModelProperty} from '@nestjs/swagger';
export class Address {

    @ApiModelProperty({description: 'Address Line 1', required : true})
    @IsString()
    @IsNotEmpty()
    required : true
    address1: string;

    @ApiModelProperty({description: 'Address Line 2', required :false})
    @IsString()
    address2?: string;
}
Run Code Online (Sandbox Code Playgroud)
// App.js: Application file where validation pipes are defined.
async function bootstrap() {
    const expressServer = express();

    const app = await NestFactory.create(AppModule, expressServer, {bodyParser: true});
    app.use(bodyParser.json({limit: 6851000}));

    app.useGlobalInterceptors(new …
Run Code Online (Sandbox Code Playgroud)

class-validator nestjs

2
推荐指数
1
解决办法
2万
查看次数

标签 统计

nestjs ×2

class-validator ×1

dynamic ×1

import ×1

module ×1

typescript ×1