1 00:00:00,880 --> 00:00:04,330 In this video we're going to show you a slightly better way of riding out this ability is our function 2 00:00:04,390 --> 00:00:06,700 so that's not a separate standalone function. 3 00:00:06,700 --> 00:00:12,850 Instead we're going to get this thing built in or included with our user model the end goal here. 4 00:00:12,850 --> 00:00:15,440 What we're really going forward is to kind of cut to the chase. 5 00:00:15,490 --> 00:00:23,880 What I really want to do is used to say user dot build and then pass in an email and a password. 6 00:00:23,910 --> 00:00:25,080 The nice thing about this approach. 7 00:00:25,110 --> 00:00:29,850 If we could get it to work we no longer have to export a separate thing from this file and create a 8 00:00:29,850 --> 00:00:33,010 new user is kind of a natural thing we say user built. 9 00:00:33,060 --> 00:00:34,800 Build me a new user create a new one. 10 00:00:34,980 --> 00:00:40,200 It makes a lot of sense and it possibly even makes more sense than this approach over here where we 11 00:00:40,200 --> 00:00:45,120 say new user it's just all a bit easier to understand what is going on because we've got that build 12 00:00:45,150 --> 00:00:46,780 method named in there. 13 00:00:46,920 --> 00:00:51,480 So that's the refactor or the change we're going to make we're going to figure out how to build in or 14 00:00:51,480 --> 00:00:59,270 include this build user function on the User model itself so let me show you how we do that to add in 15 00:00:59,360 --> 00:01:01,700 a new method to our user model. 16 00:01:01,700 --> 00:01:08,780 So really that thing right there we're going to take a look back at our user schema right above where 17 00:01:08,780 --> 00:01:10,480 we create the new user or the User model. 18 00:01:10,490 --> 00:01:18,070 I'm going to write out user schema dot statics dot built and then I'm going to assign to that the same 19 00:01:18,070 --> 00:01:19,990 function that we wrote down a little bit ago. 20 00:01:19,990 --> 00:01:21,600 So this one right here. 21 00:01:21,600 --> 00:01:29,740 So we'll say adders is user adders and then return new user with adders. 22 00:01:29,930 --> 00:01:35,490 I'm now going to remove the old build the user and delete it from the expert list as well. 23 00:01:35,510 --> 00:01:42,170 So this is how we get a custom function built into a model we add it to this statics property on our 24 00:01:42,170 --> 00:01:45,210 schema now it definitely appears like this works. 25 00:01:45,210 --> 00:01:47,040 But let's try to actually use it. 26 00:01:47,040 --> 00:01:53,870 So a quick test right here I'll say user dot builds and sure enough I still end up getting an error. 27 00:01:53,990 --> 00:01:58,580 So this is yet another point at which getting TypeScript and Mongoose to work together is just a little 28 00:01:58,580 --> 00:02:03,830 bit awkward typescript does not understand what it means to assign a property to the statics object 29 00:02:04,460 --> 00:02:08,470 if we were making use of JavaScript everything would work just fine and to be truthful with you. 30 00:02:08,480 --> 00:02:11,330 This right here it is accurate code everything we have. 31 00:02:11,330 --> 00:02:11,980 Totally accurate. 32 00:02:11,990 --> 00:02:18,440 This is how we add a function to a use it to a model in Mongoose but typescript just doesn't understand 33 00:02:18,440 --> 00:02:21,970 that we have to give a little bit more information to typescript. 34 00:02:22,040 --> 00:02:24,470 So let me show you how we're going to fix this. 35 00:02:24,560 --> 00:02:27,140 I'm going to go back up to the top of the file again. 36 00:02:27,140 --> 00:02:32,540 I'm going to add in yet another comment and I'm going to say an interface that describes the 37 00:02:35,520 --> 00:02:40,200 properties that a user model has. 38 00:02:40,200 --> 00:02:44,610 So we're gonna write out an interface that's going to essentially tell typescript that there's going 39 00:02:44,610 --> 00:02:48,620 to be a build function available on this user model. 40 00:02:48,820 --> 00:02:52,770 We're going to say that there is a built function and we're gonna tell it what arguments are required 41 00:02:52,950 --> 00:03:07,120 to call it as well I'm going to write out interface user model I'm gonna say extends Mongoose dot model 42 00:03:07,930 --> 00:03:11,770 we're going to take a look at this existing interface right here I know there's an error there don't 43 00:03:11,770 --> 00:03:16,320 worry about that we'll fix up in just a moment I want to take all the properties that already exist 44 00:03:16,320 --> 00:03:20,700 on that interface I want to add them into this new one that we are creating and I want to add in some 45 00:03:20,700 --> 00:03:26,070 new properties on top that we are getting an error from this right now to fix that error we're gonna 46 00:03:26,070 --> 00:03:30,570 put in a set of angled brackets and then write any inside there we're gonna come back and discuss what's 47 00:03:30,570 --> 00:03:36,060 going on with that just a moment but right now we'll just leave in any then on this interface we're 48 00:03:36,060 --> 00:03:37,670 going to write in that build method. 49 00:03:37,740 --> 00:03:42,510 And again this is going to tell typescript about the instance of the build method and what properties 50 00:03:42,540 --> 00:03:44,800 it accepts inside of here. 51 00:03:44,800 --> 00:03:49,130 We'll write out build it's going to take an argument called adders. 52 00:03:49,490 --> 00:03:51,800 It must be of type user adders 53 00:03:55,040 --> 00:03:58,860 and from that we are going to return right now. 54 00:03:58,970 --> 00:04:03,930 Just any we're gonna come back and fix up that any as well in just a moment. 55 00:04:05,020 --> 00:04:10,250 So now that we've got this interface put together typescript can understand about what a real user model 56 00:04:10,250 --> 00:04:11,150 is. 57 00:04:11,300 --> 00:04:16,760 All we have to do is really tell typescript about the existence of this interface so I'm going to go 58 00:04:16,760 --> 00:04:21,510 back down to where we created our Mongoose model right here. 59 00:04:21,590 --> 00:04:28,190 I'm gonna put a set of angled brackets after model I'm gonna write an any and then user model and as 60 00:04:28,190 --> 00:04:34,050 soon as I do so you'll notice that the era at year changes typescript is no longer complaining that 61 00:04:34,050 --> 00:04:37,330 we're trying to access a build property on user anymore. 62 00:04:37,380 --> 00:04:40,800 Instead it's transferred the air to the set of really braces. 63 00:04:40,820 --> 00:04:44,610 So now it's telling us hey if you want to create a new user or if you want to call this build method 64 00:04:44,790 --> 00:04:47,410 you must pass in an email and a password. 65 00:04:47,430 --> 00:04:55,770 Now if I put in an email of test at test dot com and a password of password those errors go away so 66 00:04:55,800 --> 00:05:01,530 we've now told typescript about this additional property that the user model is going to have a definite 67 00:05:01,530 --> 00:05:03,060 looks like it works out. 68 00:05:03,330 --> 00:05:08,430 Now even though this works unfortunately we just introduced a bunch of kind of mystery syntax right 69 00:05:08,430 --> 00:05:14,200 here right here and we've got that any right there as well. 70 00:05:14,870 --> 00:05:19,710 So again it works but while we've introduced a bit more confusion and unfortunately this is kind of 71 00:05:19,750 --> 00:05:20,230 that stuff. 72 00:05:20,280 --> 00:05:24,590 As mentioned earlier we're getting the typescript and Mongoose to work together involves a little bit 73 00:05:24,590 --> 00:05:26,020 of kind of crazy code. 74 00:05:26,180 --> 00:05:30,560 It definitely works and we've got some awesome type checking included now but there's kind of a mental 75 00:05:30,560 --> 00:05:31,770 cost so to speak. 76 00:05:31,770 --> 00:05:37,410 Included with it in addition to that we have really only solved issue number one right now. 77 00:05:37,460 --> 00:05:43,010 We have not really solved anything related to issue number two where we needed to somehow tell typescript 78 00:05:43,040 --> 00:05:47,420 and Mongoose that the set of properties that we pass into creating a new user are different than the 79 00:05:47,420 --> 00:05:51,240 properties that end up on a user OK. 80 00:05:51,270 --> 00:05:52,790 So still a little bit to get through here. 81 00:05:52,920 --> 00:05:55,910 Right now I'm going to delete that user dot build because I was just a quick test. 82 00:05:56,010 --> 00:05:56,910 I'm going to save this file. 83 00:05:56,910 --> 00:05:59,870 We're gonna take a quick pause and continue again in just a moment.