The last video we established that we want to remap this underscore I.D. key name just to just I.D.
And we want to remove password and underscore underscore the we could definitely write out some code
directly inside of our response right here or the response we send back whenever a user signs up.
But that would mean that we have to duplicate that code to any other location where we intend to send
a user back to ever made the request.
So in other words we'd have to duplicate that logic over to current user and probably sign in as well
because chances are those are root handlers that are going to send back the user who just signed in
or the one who is signed in.
So we should figure out a better way or more reusable way to make sure that we can remap those properties
and delete some properties as well.
So to handle this I want to give you a quick reminder on how javascript turns objects into a Jason data
might not seem super relevant when I say that but trust me it very much as relevant I'm going to open
up a quick javascript console inside my browser and just write out a little bit of code just give you
a very quick reminder.
I'm gonna create a new object inside of your called person and I'll give them a name of Alex and then
going to turn that into Jason by doing it at Jason dot string of Fi and pass in person.
All right.
No big surprise there.
As usual we just are getting the Jason representation of the object person.
So it's got name of Alex.
It turns out that we can actually override how javascript turns an object into Jason.
Let me show you how I'm going to define a second person.
So person to this person is going to have a name of also Alex and I'm going to also define a method
inside of here called to Jason.
And notice that it is all capitals.
Jason here's my method I'm gonna make sure I've got the correct number of curly braces.
So my implementation would go inside of here whenever we call Jason dot string of PHI on person to rather
than just trying to string a by the entire object javascript is going to instead invoke this to Jason
method so whatever logic we put inside there whatever we return from that function right there is going
to be the Jason representation of this object as opposed to the default behavior which is to just string
a file all the keys and values and so on he wants something really interesting.
We could return one now whenever we try to string a five person to rather than getting back a string
with a name of Alex we're going to instead run this method it's going to return one and so one is gonna
be our Jason representation of person two.
I'll do a Jason string of fi person to and look at that.
That is definitely not an accurate representation of the string of Pi version of person 2 but it's what
we said we wanted to do.
So as you would guess well we can pretty much do something like this right here to make sure that we
always whenever we send some Jason data tied to a user back to the browser or in a response we can define
that to Jason method and then customize exactly what we return.
So we can remap some properties we can delete properties and so on.
So how do we actually do that.
How do we actually apply this to our user document right here.
Well we're going to go back over to our models directory find the user dot t s file here it is right
here I'm going to go down to where we create our schema I'm gonna add in a second argument that's gonna
be an object inside this argument I'm going to add in to Jason
to chase on the inside of here works a little bit differently than what I just showed you at the console.
Remember at the console if I go back over to my browser and I deleted that example there we go right
here.
So at the console we defined a two Jason function tied to an object.
The same idea is still it related or relevant to the world of mongoose.
It's just implemented slightly slightly differently.
So inside the second object right here we're going to define to Jason.
And rather than being a function instead it is going to be an object inside of here.
We're going to define a set of properties that are going to help Mongoose take our user document and
turn it into Jason.
So again it is not quite identical to what I just showed you inside the console where we just defined
one function.
Instead we get this object we can pass in a set of different customization options that describe how
to turn our document into Jason to see a full list of how we can do that customization.
We can do a command lick so on your keyboard hold down command and then click on to Jason.
This is going to take you to the definition of what to Jason should be we can then do another command
click or control click.
If you are on Windows and click on document to object options so these are all the properties we can
list out inside that object to somehow customize how our document gets turned in to Jason so we can
set up some different getters we can remove some empty and empty objects and some other stuff inside
of here as well okay.
So with then mind I do want to point out one particular option called transform that's the option we
are going to use to kind of massage how our user document it's turned into Jason.
It is similar in nature to the to Jason function we just saw inside of the browser.
It just works a little bit differently.
In particular take a look at the arguments on here and the documentation right above it.
The first argument is referred to as Doc.
This is going to be the actual documents or the actual user instance that we are turning in to Jason.
The second argument is referred to as Rhett Rhett is short for returned object or something similar
to that.
This is a kind of pre or a attempt at turning this thing into Jason by Mongoose it's mongoose is going
to try to turn this thing into Jason for us.
So Rhett is going to be just the properties that are tied to our user document.
So for example just that idea just that email and just the password if we want to make any further changes
to how mongoose is going to turn this thing into Jason we are going to modify that red object in place.
So we are going to directly modify that red object and whatever direct changes we make to that red object
those changes will be reflected in the final Jason fied version.
Now a lot of stuff sounds kind of complicated such as write some code and I think you'll get a good
idea of what's going on really quickly okay.
So back inside my user Jess file we've got to Jason I'm going to put in transform we're going to get
in arguments of dark and reds.
So again this is the actual user document.
This is the thing that's going to eventually be turned into Jason.
So we're going to make some direct changes to that thing right there.
We're not trying to return a new object or anything like that.
We're making direct changes to that object let's first try just to just remove the password property
because we do not want the password property to show up inside of any Jason representation.
So to do so we would write out delete ret dot password now delete right here is a keyword that you do
not see very often in JavaScript the delete keyword is going to remove a property off of an object.
This is normal javascript right here.
This is not typescript or something special like that you can use the delete keyword with normal plain
old javascript that will completely remove the password property don't save this.
We're gonna go and sign up for a new user with post man once again and you'll see right away that we're
not going to get back a password property in our response.
So back over with post man going to enter in a new unique email it send and there we go there's my response
so you'll now see that we do not have any password inside there so we can repeat this process to remove
underscore underscore V and to rename I.D. as well.
Let's try removing that underscore underscore V so we could do a delete ret underscore underscore so
make sure you've got two right there the I'll save this we'll do just another very quick test so I'll
go and create another user but in the unique email and all right that's even looking better than just
you know if we look really carefully at the two Jace on options so we're just looking at a moment ago
that underscore underscore B thing is what is known as a version key so there actually is a separate
object or option right here to say that we do not want to include that version key so if we wanted to
we could absolutely add that thing or just set version key to false to not include it or alternatively
we could just do what we are doing right here and delete it manually either one is totally fine OK so
then finally last thing we need to do is remap that I.D. property remember we need to make changes to
this red object which means that remapping or renaming that property is going to actually require two
separate lines the first we're gonna set red dot I.D. to whatever the underscore I.T. property is and
then we will delete ret underscore I.D. like so OK so let's save that go back over once again change
that email send it off and there we go we're now left with simply email and I.D. So now we don't see
any properties that we do not want to see in addition I.D. is using the more consistent form of just
I.D. as opposed to underscore I.D. All right so it looks way better we're going to end up repeating
this approach in several other services as well anytime that we need to take a resource and turn it
into plain Jason there's other thing one other thing I want to mention very quickly it is a typical.
In other words it is not super common to put together some logic like this inside of a model definition
file what we're really doing right here is defining how some data inside of application should be viewed
or transmitted along the network it is not common to define that inside of something like this right
here or inside of our model file instead that is more traditionally kind of like a view related responsibility.
So for trying to follow a model view controller kind of concept what we're doing right here is really
writing out some view lever let view level logic so again not the best approach but for our purposes
this is going to work very easily for right now okay.
That's definitely going to prevent.
Let's take a quick pause here and continue in just a moment.