简体   繁体   English

在Nest JS中导入dto类

[英]Importing dto class in Nest JS

在此处输入图片说明 I'm trying to use a DTO to define my data for my controller within Nest.js. 我正在尝试使用DTO为Nest.js中的控制器定义数据。

I am following the tutorial 我正在按照教程

I have created my DTO within src/controllers/cats/dto/create-cat.dto.js 我已经在src/controllers/cats/dto/create-cat.dto.js创建了DTO

export class CreateCatDto {
  readonly name: string;
  readonly age: number;
  readonly breed: string;
}

I am confused as to how this gets imported into the application though. 我对于如何将其导入应用程序感到困惑。 The documentation doesn't actually state that it needs to be imported so I assumed nest is doing some magic behind the scenes? 该文档实际上并没有指出需要将其导入,因此我认为nest在幕后做了一些魔术? All though I have a feeling this isn't the case. 尽管我有种感觉,但事实并非如此。

I am trying to import it directly in my controller: 我试图将其直接导入我的控制器中:

import { CreateCatDto } from './dto/create-cat.dto';

But this throws an error: 但这会引发错误:

Unexpected token (2:11)
  1 | export class CreateCatDto {
> 2 |   readonly name: string;
    |            ^
  3 |   readonly age: number;
  4 |   readonly breed: string;
  5 | }

The DTO code is ripped directly off the nest docs so there shouldn't be an ussue with the code (all though readonly name: string; doesn't look like javascript I have come across before). DTO代码是直接从嵌套文档中剥离出来的,因此代码不应有什么用处(尽管readonly name: string;看起来好像不是我以前遇到的javascript)。

For reference, here is the rest of my cat Controller where I am trying to use the DTO 供参考,这是我尝试使用DTO的cat控制器的其余部分

import { Controller, Bind, Get, Post, Body, Res, HttpStatus } from '@nestjs/common';
// import { CreateCatDto } from './dto/create-cat.dto';

@Controller('cats')
export class CatsController {

  @Post()
    @Bind(Res(), Body())
    async create(res, body, createCatDto) {
        console.log("createCatDto", createCatDto)
        res.status(HttpStatus.CREATED).send();
    }

  @Get()
  findAll() {
    return [];
  }
}

Does the DTO class need to be imported and then use bind to my create function like Res() and Body() or does nest do some magic behind the scene since they never state to import it in there docs? DTO类是否需要导入,然后使用绑定到我的create函数(如Res()Body()还是nest在幕后做些魔术,因为它们从未声明过将其导入那里的文档?

Thanks. 谢谢。

Quick Answer: You can't use DTO in JavaScript ES6 快速解答:您不能在JavaScript ES6中使用DTO

Not So Long Answer: Here is an excerpt from the docs, just has DTO was introduced. 不太长的答案:这是摘录自文档,只是介绍了DTO。

Firstly, we need to establish the DTO (Data Transfer Object) schema. 首先,我们需要建立DTO(数据传输对象)架构。 A DTO is an object that defines how the data will be sent over the network. DTO是定义如何在网络上发送数据的对象。 We could do this by using TypeScript interfaces, or by simple classes. 我们可以通过使用TypeScript接口或简单的类来实现。

As you can see, it was used there as an interface to provide Static typing . 如您所见,它在那里被用作提供静态类型接口 You really can't utilize DTOs in JS because interface and Static typing are not part of ES6 standards. 您真的不能在JS中使用DTO,因为接口静态类型化不是ES6标准的一部分。

You should skip the DTO part for ES6 and use the @Body() argument like this instead 您应该跳过ES6的DTO部分,而是使用@Body()参数代替

@Post()
@Bind(Body())
async create(createCatDto) {
    // TODO: Add some logic here
}

My Advice: Try considering moving to Typescript to take full advantage of NestJS. 我的建议:尝试考虑使用Typescript以充分利用NestJS。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM