1 00:00:01,580 --> 00:00:07,060 The last video we established that we want to remap this underscore I.D. key name just to just I.D. 2 00:00:07,340 --> 00:00:12,220 And we want to remove password and underscore underscore the we could definitely write out some code 3 00:00:12,280 --> 00:00:17,420 directly inside of our response right here or the response we send back whenever a user signs up. 4 00:00:17,590 --> 00:00:22,240 But that would mean that we have to duplicate that code to any other location where we intend to send 5 00:00:22,270 --> 00:00:25,000 a user back to ever made the request. 6 00:00:25,000 --> 00:00:30,700 So in other words we'd have to duplicate that logic over to current user and probably sign in as well 7 00:00:30,790 --> 00:00:35,020 because chances are those are root handlers that are going to send back the user who just signed in 8 00:00:35,050 --> 00:00:36,280 or the one who is signed in. 9 00:00:37,210 --> 00:00:42,130 So we should figure out a better way or more reusable way to make sure that we can remap those properties 10 00:00:42,130 --> 00:00:44,070 and delete some properties as well. 11 00:00:45,200 --> 00:00:51,800 So to handle this I want to give you a quick reminder on how javascript turns objects into a Jason data 12 00:00:52,670 --> 00:00:57,760 might not seem super relevant when I say that but trust me it very much as relevant I'm going to open 13 00:00:57,760 --> 00:01:02,020 up a quick javascript console inside my browser and just write out a little bit of code just give you 14 00:01:02,020 --> 00:01:03,600 a very quick reminder. 15 00:01:03,730 --> 00:01:10,530 I'm gonna create a new object inside of your called person and I'll give them a name of Alex and then 16 00:01:10,530 --> 00:01:16,310 going to turn that into Jason by doing it at Jason dot string of Fi and pass in person. 17 00:01:16,410 --> 00:01:16,790 All right. 18 00:01:16,830 --> 00:01:17,690 No big surprise there. 19 00:01:17,700 --> 00:01:21,900 As usual we just are getting the Jason representation of the object person. 20 00:01:21,930 --> 00:01:24,260 So it's got name of Alex. 21 00:01:24,420 --> 00:01:29,810 It turns out that we can actually override how javascript turns an object into Jason. 22 00:01:30,030 --> 00:01:33,300 Let me show you how I'm going to define a second person. 23 00:01:33,490 --> 00:01:42,940 So person to this person is going to have a name of also Alex and I'm going to also define a method 24 00:01:42,940 --> 00:01:45,580 inside of here called to Jason. 25 00:01:45,730 --> 00:01:47,790 And notice that it is all capitals. 26 00:01:47,800 --> 00:01:54,700 Jason here's my method I'm gonna make sure I've got the correct number of curly braces. 27 00:01:54,700 --> 00:02:01,330 So my implementation would go inside of here whenever we call Jason dot string of PHI on person to rather 28 00:02:01,330 --> 00:02:07,300 than just trying to string a by the entire object javascript is going to instead invoke this to Jason 29 00:02:07,360 --> 00:02:13,990 method so whatever logic we put inside there whatever we return from that function right there is going 30 00:02:13,990 --> 00:02:18,730 to be the Jason representation of this object as opposed to the default behavior which is to just string 31 00:02:18,730 --> 00:02:23,720 a file all the keys and values and so on he wants something really interesting. 32 00:02:23,720 --> 00:02:31,550 We could return one now whenever we try to string a five person to rather than getting back a string 33 00:02:31,550 --> 00:02:37,570 with a name of Alex we're going to instead run this method it's going to return one and so one is gonna 34 00:02:37,580 --> 00:02:40,610 be our Jason representation of person two. 35 00:02:40,850 --> 00:02:46,830 I'll do a Jason string of fi person to and look at that. 36 00:02:46,850 --> 00:02:52,370 That is definitely not an accurate representation of the string of Pi version of person 2 but it's what 37 00:02:52,370 --> 00:02:54,110 we said we wanted to do. 38 00:02:54,440 --> 00:02:59,390 So as you would guess well we can pretty much do something like this right here to make sure that we 39 00:02:59,990 --> 00:03:06,530 always whenever we send some Jason data tied to a user back to the browser or in a response we can define 40 00:03:06,530 --> 00:03:10,840 that to Jason method and then customize exactly what we return. 41 00:03:10,970 --> 00:03:15,410 So we can remap some properties we can delete properties and so on. 42 00:03:16,190 --> 00:03:17,300 So how do we actually do that. 43 00:03:17,300 --> 00:03:21,960 How do we actually apply this to our user document right here. 44 00:03:21,960 --> 00:03:26,970 Well we're going to go back over to our models directory find the user dot t s file here it is right 45 00:03:26,970 --> 00:03:35,460 here I'm going to go down to where we create our schema I'm gonna add in a second argument that's gonna 46 00:03:35,460 --> 00:03:40,340 be an object inside this argument I'm going to add in to Jason 47 00:03:43,030 --> 00:03:47,750 to chase on the inside of here works a little bit differently than what I just showed you at the console. 48 00:03:47,790 --> 00:03:55,100 Remember at the console if I go back over to my browser and I deleted that example there we go right 49 00:03:55,100 --> 00:03:55,940 here. 50 00:03:56,000 --> 00:04:00,970 So at the console we defined a two Jason function tied to an object. 51 00:04:01,160 --> 00:04:04,880 The same idea is still it related or relevant to the world of mongoose. 52 00:04:04,910 --> 00:04:08,220 It's just implemented slightly slightly differently. 53 00:04:08,450 --> 00:04:11,420 So inside the second object right here we're going to define to Jason. 54 00:04:11,420 --> 00:04:16,540 And rather than being a function instead it is going to be an object inside of here. 55 00:04:16,550 --> 00:04:20,990 We're going to define a set of properties that are going to help Mongoose take our user document and 56 00:04:20,990 --> 00:04:22,880 turn it into Jason. 57 00:04:22,880 --> 00:04:26,810 So again it is not quite identical to what I just showed you inside the console where we just defined 58 00:04:26,810 --> 00:04:27,800 one function. 59 00:04:27,860 --> 00:04:33,560 Instead we get this object we can pass in a set of different customization options that describe how 60 00:04:33,560 --> 00:04:38,370 to turn our document into Jason to see a full list of how we can do that customization. 61 00:04:38,510 --> 00:04:44,420 We can do a command lick so on your keyboard hold down command and then click on to Jason. 62 00:04:44,510 --> 00:04:50,160 This is going to take you to the definition of what to Jason should be we can then do another command 63 00:04:50,160 --> 00:04:51,390 click or control click. 64 00:04:51,510 --> 00:04:57,760 If you are on Windows and click on document to object options so these are all the properties we can 65 00:04:57,760 --> 00:05:04,210 list out inside that object to somehow customize how our document gets turned in to Jason so we can 66 00:05:04,210 --> 00:05:10,900 set up some different getters we can remove some empty and empty objects and some other stuff inside 67 00:05:10,900 --> 00:05:14,320 of here as well okay. 68 00:05:14,510 --> 00:05:20,180 So with then mind I do want to point out one particular option called transform that's the option we 69 00:05:20,180 --> 00:05:25,430 are going to use to kind of massage how our user document it's turned into Jason. 70 00:05:25,610 --> 00:05:30,070 It is similar in nature to the to Jason function we just saw inside of the browser. 71 00:05:30,140 --> 00:05:32,650 It just works a little bit differently. 72 00:05:32,990 --> 00:05:36,990 In particular take a look at the arguments on here and the documentation right above it. 73 00:05:37,100 --> 00:05:39,700 The first argument is referred to as Doc. 74 00:05:39,830 --> 00:05:46,060 This is going to be the actual documents or the actual user instance that we are turning in to Jason. 75 00:05:46,090 --> 00:05:51,520 The second argument is referred to as Rhett Rhett is short for returned object or something similar 76 00:05:51,520 --> 00:05:52,740 to that. 77 00:05:52,810 --> 00:05:58,810 This is a kind of pre or a attempt at turning this thing into Jason by Mongoose it's mongoose is going 78 00:05:58,810 --> 00:06:01,530 to try to turn this thing into Jason for us. 79 00:06:01,690 --> 00:06:06,370 So Rhett is going to be just the properties that are tied to our user document. 80 00:06:06,370 --> 00:06:12,340 So for example just that idea just that email and just the password if we want to make any further changes 81 00:06:12,520 --> 00:06:18,850 to how mongoose is going to turn this thing into Jason we are going to modify that red object in place. 82 00:06:18,880 --> 00:06:24,760 So we are going to directly modify that red object and whatever direct changes we make to that red object 83 00:06:25,120 --> 00:06:29,620 those changes will be reflected in the final Jason fied version. 84 00:06:29,650 --> 00:06:33,250 Now a lot of stuff sounds kind of complicated such as write some code and I think you'll get a good 85 00:06:33,250 --> 00:06:36,020 idea of what's going on really quickly okay. 86 00:06:36,080 --> 00:06:45,000 So back inside my user Jess file we've got to Jason I'm going to put in transform we're going to get 87 00:06:45,000 --> 00:06:47,370 in arguments of dark and reds. 88 00:06:47,430 --> 00:06:49,770 So again this is the actual user document. 89 00:06:49,770 --> 00:06:52,710 This is the thing that's going to eventually be turned into Jason. 90 00:06:52,830 --> 00:06:55,830 So we're going to make some direct changes to that thing right there. 91 00:06:55,860 --> 00:06:58,370 We're not trying to return a new object or anything like that. 92 00:06:58,410 --> 00:07:05,240 We're making direct changes to that object let's first try just to just remove the password property 93 00:07:05,270 --> 00:07:09,990 because we do not want the password property to show up inside of any Jason representation. 94 00:07:10,160 --> 00:07:17,070 So to do so we would write out delete ret dot password now delete right here is a keyword that you do 95 00:07:17,070 --> 00:07:23,370 not see very often in JavaScript the delete keyword is going to remove a property off of an object. 96 00:07:23,430 --> 00:07:24,870 This is normal javascript right here. 97 00:07:24,870 --> 00:07:30,360 This is not typescript or something special like that you can use the delete keyword with normal plain 98 00:07:30,390 --> 00:07:37,760 old javascript that will completely remove the password property don't save this. 99 00:07:37,760 --> 00:07:42,380 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 100 00:07:42,380 --> 00:07:46,530 not going to get back a password property in our response. 101 00:07:46,570 --> 00:07:53,740 So back over with post man going to enter in a new unique email it send and there we go there's my response 102 00:07:54,100 --> 00:07:59,680 so you'll now see that we do not have any password inside there so we can repeat this process to remove 103 00:07:59,740 --> 00:08:03,450 underscore underscore V and to rename I.D. as well. 104 00:08:04,590 --> 00:08:10,930 Let's try removing that underscore underscore V so we could do a delete ret underscore underscore so 105 00:08:10,940 --> 00:08:19,350 make sure you've got two right there the I'll save this we'll do just another very quick test so I'll 106 00:08:19,540 --> 00:08:27,170 go and create another user but in the unique email and all right that's even looking better than just 107 00:08:27,170 --> 00:08:31,490 you know if we look really carefully at the two Jace on options so we're just looking at a moment ago 108 00:08:32,030 --> 00:08:37,280 that underscore underscore B thing is what is known as a version key so there actually is a separate 109 00:08:37,310 --> 00:08:42,350 object or option right here to say that we do not want to include that version key so if we wanted to 110 00:08:42,350 --> 00:08:48,950 we could absolutely add that thing or just set version key to false to not include it or alternatively 111 00:08:48,950 --> 00:08:55,040 we could just do what we are doing right here and delete it manually either one is totally fine OK so 112 00:08:55,040 --> 00:09:00,680 then finally last thing we need to do is remap that I.D. property remember we need to make changes to 113 00:09:00,680 --> 00:09:05,480 this red object which means that remapping or renaming that property is going to actually require two 114 00:09:05,480 --> 00:09:13,070 separate lines the first we're gonna set red dot I.D. to whatever the underscore I.T. property is and 115 00:09:13,070 --> 00:09:23,760 then we will delete ret underscore I.D. like so OK so let's save that go back over once again change 116 00:09:23,760 --> 00:09:33,150 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 117 00:09:33,150 --> 00:09:39,270 any properties that we do not want to see in addition I.D. is using the more consistent form of just 118 00:09:39,270 --> 00:09:45,130 I.D. as opposed to underscore I.D. All right so it looks way better we're going to end up repeating 119 00:09:45,130 --> 00:09:49,690 this approach in several other services as well anytime that we need to take a resource and turn it 120 00:09:49,690 --> 00:09:55,470 into plain Jason there's other thing one other thing I want to mention very quickly it is a typical. 121 00:09:55,480 --> 00:10:01,690 In other words it is not super common to put together some logic like this inside of a model definition 122 00:10:01,690 --> 00:10:07,450 file what we're really doing right here is defining how some data inside of application should be viewed 123 00:10:07,480 --> 00:10:13,030 or transmitted along the network it is not common to define that inside of something like this right 124 00:10:13,030 --> 00:10:19,720 here or inside of our model file instead that is more traditionally kind of like a view related responsibility. 125 00:10:19,720 --> 00:10:24,310 So for trying to follow a model view controller kind of concept what we're doing right here is really 126 00:10:24,310 --> 00:10:31,090 writing out some view lever let view level logic so again not the best approach but for our purposes 127 00:10:31,180 --> 00:10:34,510 this is going to work very easily for right now okay. 128 00:10:34,550 --> 00:10:35,760 That's definitely going to prevent. 129 00:10:35,860 --> 00:10:38,500 Let's take a quick pause here and continue in just a moment.