Hi everyone.
Before I go into the validation step, I want to talk a little bit more about "DTO".
Now in previous video we already see, when we send the data we will send the "DTO", right?
And when we response the data from the server we need the "DTO".
Because you can see in here we don't need to check the "Id" or the or actually we need the ID, but we
don't need the "__v" right here.
Right?
I don't want that
So I can easily remove that using the DTO.
And you can see right here, we can easily do something like, like this Right here.
We will save that into the variable.
Right.
data = await
Sorry, it's same name.
I will use the "post" right here.
And with the "post" I can return the title.
What's the title and description?
post.description
Right.
We can do that.
And let's see I will undo
Here you can say very good.
Right.
And if you want the "id", you can say the _id: post._id, right here
It also return. But let me write it in the best way with the "DTO".
So in the DTO here I will create one more detail called the.
response-post.dto.ts
And I will copy the class right here, paste it to here because it's very similar, response post
DTO
Okay, this is the class.
And in here, I can write a helper methods to convert the entities into the class.
But right now I will not do that.
I will show you the easy way right here.
I will create a postDto = new ResponsePostDTO()
Post DTO
And with the postDto, I can use postDto.title
= post.title
post
._id
Sorry.
post
post.title, postDto.description
post.description
And if we need the ID, we will provide the ID right here.
string
postDto.id actually "_id" is best.
= post.
post
._id
And we will return.
Oh because the "_id" here is the "TypeId" and we can convert this using string if you want.
Okay.
I will convert this .toString() here
Okay.
That's the purpose of the "DTO".
And I will return right here.
Now you can see what happened right now I can use the typescript right here okay.
Promise.
With the ResponsePostDTO
Right.
Very cool.
And because we will wrap it inside the promise.
Because we use the async.
And async always return promise.
Okay, this is the JavaScript knowledge.
If I send it again.
Very cool, right?
We have a "_id" "description", we no longer have the "__v" anymore
And actually, if you want, you can easily remove the underscore.
v
Okay.
But in the MVC architecture we usually use the "DTO"
To access when we send a request or when we return the response, we use the "DTO".
So that's it, that's for the video.
And in the next video I will show you how we can validation when we send a request.
Right.
I will see you in the next one.