admin管理员组

文章数量:1389750

I'm trying to use a DTO to define my data for my controller within Nest.js.

I am following the tutorial

I have created my DTO within src/controllers/cats/dto/create-cat.dto.js

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? 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 e across before).

For reference, here is the rest of my cat Controller where I am trying to use the DTO

import { Controller, Bind, Get, Post, Body, Res, HttpStatus } from '@nestjs/mon';
// 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?

Thanks.

I'm trying to use a DTO to define my data for my controller within Nest.js.

I am following the tutorial

I have created my DTO within src/controllers/cats/dto/create-cat.dto.js

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? 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 e across before).

For reference, here is the rest of my cat Controller where I am trying to use the DTO

import { Controller, Bind, Get, Post, Body, Res, HttpStatus } from '@nestjs/mon';
// 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?

Thanks.

Share Improve this question edited Feb 21, 2018 at 11:27 Stretch0 asked Feb 21, 2018 at 11:17 Stretch0Stretch0 9,31315 gold badges94 silver badges159 bronze badges 7
  • It's not javascript, it's TypeScript. You created JS file - src/controllers/cats/dto/create-cat.dto.js – kxyz Commented Feb 21, 2018 at 11:24
  • That's confusing because in their docs, they clearly remend using es6 over typescript as you can see in the screenshot I have added and can be found here docs.nestjs./controllers under the header POST handler – Stretch0 Commented Feb 21, 2018 at 11:28
  • cool fact is that all the docs have typescript/javascript code examples, so they gave both examples. – PlayMa256 Commented Feb 21, 2018 at 11:30
  • 1 @MatheusSilva not all, JS contains in some cases TS – kxyz Commented Feb 21, 2018 at 11:31
  • I am aware of that, but in this particular example, the code snippet is exactly the same fort JS and TS. And as the screenshot states: "we remend using classes here" so I assumed they are both JS snippets... – Stretch0 Commented Feb 21, 2018 at 11:31
 |  Show 2 more ments

1 Answer 1

Reset to default 6

Quick Answer: You can't use DTO in JavaScript ES6

Not So Long Answer: Here is an excerpt from the docs, just has DTO was introduced.

Firstly, we need to establish the DTO (Data Transfer Object) schema. A DTO is an object that defines how the data will be sent over the network. We could do this by using TypeScript interfaces, or by simple classes.

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.

You should skip the DTO part for ES6 and use the @Body() argument like this instead

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

My Advice: Try considering moving to Typescript to take full advantage of NestJS.

本文标签: javascriptImporting dto class in Nest JSStack Overflow