1 00:00:00,480 --> 00:00:07,860 Let's look at what we just did in the previous video. We've made our database communicate with the server, 2 00:00:07,950 --> 00:00:11,300 and we've registered our first user which was Ann. 3 00:00:11,660 --> 00:00:16,440 But now we want to respond to the Front-End with this new user, 4 00:00:16,440 --> 00:00:18,340 Ann. So how can we do that. 5 00:00:18,660 --> 00:00:25,320 Well if this is success, in which case we want to do a dot then,which we have here. 6 00:00:29,080 --> 00:00:34,590 If we get a response, in that case we want to 7 00:00:37,360 --> 00:00:45,070 send a response to Json and this response dot json will respond with... 8 00:00:45,270 --> 00:00:48,100 Well what should it respond with? 9 00:00:48,140 --> 00:00:56,570 Luckily for us KNEX again is built with this in mind, and has a feature that is - or a method that 10 00:00:56,570 --> 00:01:00,300 is called returning. 11 00:01:00,640 --> 00:01:07,230 So instead of us doing another select statement and grabbing Ann we can just say returning all. 12 00:01:07,540 --> 00:01:14,890 So this says that users insert Ann and return all the columns. 13 00:01:14,890 --> 00:01:16,210 Let's see if that works. 14 00:01:16,420 --> 00:01:22,730 If that works I can just place the response here. 15 00:01:22,910 --> 00:01:23,550 Let's check it out. 16 00:01:25,800 --> 00:01:28,960 Go into save. Open our postman. 17 00:01:29,390 --> 00:01:42,290 I'm going to add a new user, we'll just say Anne with an e. Hit send, like that. I get an array that is Anne. 18 00:01:44,290 --> 00:01:44,800 Amazing. 19 00:01:44,830 --> 00:01:45,810 Looks like it's working. 20 00:01:45,820 --> 00:01:54,950 If I go back to the database and do select star from users there's two users but the one that I've received 21 00:01:55,280 --> 00:02:02,360 is just Anne. Let's create a new user. I'm going to create John, cookies. 22 00:02:02,390 --> 00:02:09,610 We remember this guy. Click send. We get John. 23 00:02:09,620 --> 00:02:10,310 Amazing. 24 00:02:10,310 --> 00:02:13,950 Let's check with the front end to see that this actually works. 25 00:02:14,120 --> 00:02:20,840 If I go back here - let's just change this into user just so it's more descriptive. 26 00:02:20,840 --> 00:02:28,440 So we're returning the user but we also want to make sure that it's not an array and we're returning 27 00:02:28,440 --> 00:02:29,010 the object. 28 00:02:29,010 --> 00:02:30,360 So we'll just do it user. 29 00:02:30,420 --> 00:02:35,730 The first one because when you register a user they should only be one. 30 00:02:35,780 --> 00:02:41,090 And we also want to make sure that if, in case there's an error, we respond. 31 00:02:41,890 --> 00:02:57,220 With a response dot Json error or perhaps you can even do a status 400. Let's give that a go - and this 32 00:02:57,220 --> 00:02:57,740 should be 33 00:02:57,760 --> 00:02:58,410 Sorry. 34 00:02:58,420 --> 00:02:59,250 Catch. 35 00:02:59,260 --> 00:03:02,560 Let's say that again - to catch any errors that may have happened here. 36 00:03:03,110 --> 00:03:10,760 If I go back here and try to register with the same John I get - look at that - an error. 37 00:03:13,010 --> 00:03:16,430 And we see that key e-mail John already exists. 38 00:03:16,520 --> 00:03:18,600 This is an important point here. 39 00:03:18,800 --> 00:03:25,110 We get a 400 error but we are returning information about our database. 40 00:03:25,170 --> 00:03:29,960 We now know that in this database John already exists. 41 00:03:30,190 --> 00:03:36,550 And one of the big security principles is that you want to make sure that you don't give the client 42 00:03:36,670 --> 00:03:38,510 any information about your system. 43 00:03:39,530 --> 00:03:41,430 And this is not good. 44 00:03:41,480 --> 00:03:53,410 So perhaps instead of returning the error we can just say "unable to join" or "unable to register". If we 45 00:03:53,410 --> 00:04:04,490 save, send again. Unable to register - 400 - bad request. Amazing. 46 00:04:04,500 --> 00:04:07,370 Let's see if that actually works with our front end now. 47 00:04:07,390 --> 00:04:16,290 I go back to my app click on register and let's register with a completely new user will do Amy 48 00:04:16,680 --> 00:04:28,610 e-mail will be TheAmy@gmail.com password 1 2 3 - oops - password 1 2 3. Register. Amy your current entry 49 00:04:28,610 --> 00:04:30,400 count is zero. 50 00:04:30,420 --> 00:04:32,700 How cool is that. 51 00:04:32,730 --> 00:04:34,650 And just to make sure that Amy got registered. 52 00:04:34,650 --> 00:04:42,300 If we go back to our database and I do select or I can just press up because we've done this before 53 00:04:43,320 --> 00:04:51,090 I have Amy in my database. So cool. 54 00:04:51,310 --> 00:04:53,360 So we've done the register. 55 00:04:53,710 --> 00:04:55,080 Everything looks pretty nice. 56 00:04:55,090 --> 00:04:58,390 There's a few more endpoints to fix so let's do that. 57 00:04:58,390 --> 00:04:59,440 I'll see you in the next one. 58 00:04:59,840 --> 00:05:00,070 Bye-bye.