1 00:00:00,650 --> 00:00:03,900 In the last section we finished up our update one query. 2 00:00:04,180 --> 00:00:08,320 So now we're ready to take this query and move it back over to our route handler. 3 00:00:08,320 --> 00:00:13,090 I'm going to copy the entire query that we just wrote so I can highlight everything and copy it and 4 00:00:13,090 --> 00:00:17,770 then I'm going to change back to our survey route's file where we've been working on our web hoax route 5 00:00:17,770 --> 00:00:18,340 handler. 6 00:00:18,350 --> 00:00:19,760 Here it is right here. 7 00:00:19,960 --> 00:00:23,020 Now I'm looking at the route handler body. 8 00:00:23,020 --> 00:00:27,820 I'm trying to think of exactly where the best place is to actually write in this query. 9 00:00:28,090 --> 00:00:34,240 Well remember when we iterate through the erect body object we process all these different events and 10 00:00:34,240 --> 00:00:37,020 then eventually assign it to the events array. 11 00:00:37,020 --> 00:00:39,190 Right here are the events variable. 12 00:00:39,220 --> 00:00:41,940 So really we want to iterate over this array. 13 00:00:42,100 --> 00:00:45,890 And for every processed event we want to execute that query. 14 00:00:46,210 --> 00:00:49,810 So you might be thinking OK well let's write a loop down here. 15 00:00:49,840 --> 00:00:54,460 So rather than doing the concert log maybe we need to loop right here over the events array. 16 00:00:54,490 --> 00:00:57,050 And for every event we will issue the query. 17 00:00:57,430 --> 00:01:03,550 But remember we've already wired up this fancy chain helper right here that we can use to just do one 18 00:01:03,550 --> 00:01:06,280 more iteration step through the list of events. 19 00:01:06,460 --> 00:01:11,320 So we don't have to assign events to this intermediate variable and then iterate through it. 20 00:01:11,440 --> 00:01:17,080 We can just add on another branch to our chain right here or another step to the chain to then iterate 21 00:01:17,110 --> 00:01:22,060 over the list of events that have been calculated by these three steps right here and issue the query 22 00:01:22,060 --> 00:01:23,590 for each and every one. 23 00:01:24,130 --> 00:01:28,350 So I'm going to find my unique by statement right underneath that. 24 00:01:28,390 --> 00:01:32,320 I'm going to add in one more step called Dot each. 25 00:01:32,320 --> 00:01:38,770 And so as you might imagine this dot each function will run over every single element in the events 26 00:01:38,830 --> 00:01:39,820 array. 27 00:01:39,850 --> 00:01:45,460 So I will say for every event we want to essentially run our query. 28 00:01:45,580 --> 00:01:49,310 So inside of this or a function I'm going to paste the query that we just wrote. 29 00:01:49,390 --> 00:01:51,770 So here's the whole query right here. 30 00:01:51,790 --> 00:01:55,030 Now we just run off and try to test this out inside the browser. 31 00:01:55,030 --> 00:01:58,450 There are still a few odds and ends that we want to take care of. 32 00:01:58,510 --> 00:02:04,240 So the first thing I want to do is to make sure that I clean up the Konst events declaration up here 33 00:02:04,360 --> 00:02:06,730 because we're not actually making use of these events. 34 00:02:06,730 --> 00:02:09,180 We not actually cost logging them or anything like that. 35 00:02:09,190 --> 00:02:13,390 All we need to do is run this entire chain statement and that's it. 36 00:02:13,390 --> 00:02:17,550 So I'm not going to assign anything to the events variable anymore. 37 00:02:17,560 --> 00:02:21,420 I'm going to make sure that I clean up that console log of events down here as well. 38 00:02:21,430 --> 00:02:24,640 So I should have no other references to events. 39 00:02:24,820 --> 00:02:26,670 I guess that's cleanup's step 1. 40 00:02:26,710 --> 00:02:31,420 Now the next thing we need to do is make two small changes to the query that we just wrote the update 41 00:02:31,420 --> 00:02:32,310 one query. 42 00:02:32,650 --> 00:02:38,080 Both these are total gotchas that are just little odds and ends that made more sense to do right now 43 00:02:38,110 --> 00:02:39,160 rather than tackle. 44 00:02:39,190 --> 00:02:42,380 Over here when we are actually trying to put together the query. 45 00:02:42,900 --> 00:02:50,360 So kind of gotcha number one gotcha number one right now we are looking for an ID of survey ID. 46 00:02:50,380 --> 00:02:57,640 So gotcha number one is that in the Mongo D-B world internally in long ago all of our records are assigned 47 00:02:57,730 --> 00:03:02,980 not an id property but an underscore id property. 48 00:03:03,130 --> 00:03:09,010 And so any time that we want to specifically look for a record with a given ID by using find one or 49 00:03:09,010 --> 00:03:14,860 update one or delete one or any of these different helpers we have to say underscore Id rather than 50 00:03:14,860 --> 00:03:19,770 just Id like so now this is how things work in the Mongo world. 51 00:03:19,780 --> 00:03:25,780 The reason that we are able to use ID in many other places inside of our codebase is because mongoose. 52 00:03:25,780 --> 00:03:32,510 Remember mongooses city involved with Mago mongoose automatically will it will respect the ID property. 53 00:03:32,560 --> 00:03:38,320 However whenever you are passing a query off to Mago you have to specifically say underscore dot or 54 00:03:38,320 --> 00:03:39,690 underscore ID. 55 00:03:40,360 --> 00:03:43,680 Now if that sounds really confusing arbitrary will it kind of is. 56 00:03:43,690 --> 00:03:48,010 Let me just say that at the end of the day you can always be safe with just about all the code that 57 00:03:48,010 --> 00:03:50,670 you write by just using underscore ID everywhere. 58 00:03:50,680 --> 00:03:52,810 So it is sort of throw that out there. 59 00:03:53,080 --> 00:03:55,700 OK now small change number 2 to our query. 60 00:03:55,750 --> 00:03:58,330 So here is the opening parentheses. 61 00:03:58,560 --> 00:04:02,150 Here is the closing parentheses for the query. 62 00:04:02,430 --> 00:04:06,940 After we close off the query right here but before we close off the each statement. 63 00:04:06,960 --> 00:04:10,620 So here is the closing curly brace and parentheses for each. 64 00:04:10,770 --> 00:04:16,430 So on the query still we're going to call DOT exec like so. 65 00:04:16,440 --> 00:04:22,530 So don't exact is short for Dot execute with this update one statement that we've written right here. 66 00:04:22,560 --> 00:04:26,910 This just kind of puts equerry together but it doesn't actually execute it doesn't actually send it 67 00:04:26,910 --> 00:04:28,130 off to the database. 68 00:04:28,290 --> 00:04:32,590 So we need to make sure that we actually execute the query by calling dot exec. 69 00:04:33,090 --> 00:04:38,010 Now one last thing I think inside the query you'll notice that this each function is being called with 70 00:04:38,010 --> 00:04:39,380 the event object. 71 00:04:39,480 --> 00:04:45,150 But we really only care about the survey ID the email and the choice properties of the event. 72 00:04:45,150 --> 00:04:49,040 So rather than just plugging in the entire event we'll do a little bit of destructuring. 73 00:04:49,290 --> 00:04:59,070 So will place parentheses curly braces and I will say I only care about Servais ID email and choice 74 00:04:59,160 --> 00:04:59,750 like so. 75 00:05:01,380 --> 00:05:01,670 OK. 76 00:05:01,700 --> 00:05:06,970 Now when I save it looks like we get a nice big jump from my prettier add on. 77 00:05:07,580 --> 00:05:07,910 OK. 78 00:05:07,910 --> 00:05:09,290 So I think this is looking good. 79 00:05:09,380 --> 00:05:13,280 Now one last thing I want to mention to you because you might be a little bit confused about one thing 80 00:05:13,280 --> 00:05:18,670 that we are actually not going to do this time around this query that we are issuing right here. 81 00:05:18,710 --> 00:05:23,070 This query that we are executing it is another asynchronous piece of code. 82 00:05:23,090 --> 00:05:28,040 Its something that takes some amount of time to go over to the database and execute this little query. 83 00:05:28,040 --> 00:05:34,010 However you'll notice that we are not going to put in any async awaits or any promises or anything like 84 00:05:34,010 --> 00:05:34,530 that. 85 00:05:34,670 --> 00:05:40,790 And the reason for this is that in the context of our web Hoque right here we don't really have anything 86 00:05:40,790 --> 00:05:47,660 to respond back to send grid with really send grids web hook is send grid doing us a favor. 87 00:05:47,660 --> 00:05:49,410 They're just saying hey here is the state. 88 00:05:49,440 --> 00:05:50,340 Your ego. 89 00:05:50,480 --> 00:05:53,700 And we take that data and we do some processing on it. 90 00:05:53,720 --> 00:05:58,500 We don't have to respond to send grid with any specific stuff. 91 00:05:58,520 --> 00:06:03,550 So really once said grid sends us our data it's up to us like we're on our own at that point. 92 00:06:03,560 --> 00:06:08,450 We do not have to wait for this query to finish before we send back a response. 93 00:06:08,450 --> 00:06:13,050 And in fact centigrade and could care less if we send them back some type of response. 94 00:06:13,370 --> 00:06:18,290 So we don't really need any asynchronous handlers here because we don't need to put in any type of checks 95 00:06:18,290 --> 00:06:21,020 to make sure that oh did everything like save. 96 00:06:21,020 --> 00:06:23,890 Or is it all done or do we need to respond with the records. 97 00:06:23,910 --> 00:06:26,020 No we don't care about any of that stuff. 98 00:06:26,060 --> 00:06:29,030 We want to just attempt to apply these updates and that's it. 99 00:06:29,030 --> 00:06:29,950 Like who cares. 100 00:06:29,960 --> 00:06:31,740 That's all. 101 00:06:31,740 --> 00:06:31,940 All right. 102 00:06:31,940 --> 00:06:36,410 I think maybe one last thing inside this file we are making use of the survey model class. 103 00:06:36,590 --> 00:06:40,600 So let's make sure that we require this survey or model class at the top of the file. 104 00:06:40,850 --> 00:06:46,520 So scroll up to the top and I think that I already added in a required statement from mongoose because 105 00:06:46,520 --> 00:06:48,820 I just wanted to do one little test around the query. 106 00:06:49,160 --> 00:06:54,410 So make sure that you get the required statement for mongoose and then make sure that you also get the 107 00:06:54,410 --> 00:06:59,900 survey model class often mongoose as well with Mongoose model surveys. 108 00:06:59,900 --> 00:07:03,210 So just double check make sure you've got both those lines. 109 00:07:03,960 --> 00:07:06,750 OK time to test this thing out. 110 00:07:06,770 --> 00:07:11,840 So the first thing I'm going to do is flip on over to my terminal and I'm going to verify make sure 111 00:07:11,840 --> 00:07:15,540 that my server is still running without any big nasty error messages. 112 00:07:15,650 --> 00:07:19,100 So nothing that says like hey you got a big typo in here. 113 00:07:19,100 --> 00:07:20,360 So it looks like I'm OK. 114 00:07:20,720 --> 00:07:27,810 So I flip back over to my re-act application and I'm going to create a new survey so we'll call this 115 00:07:27,810 --> 00:07:33,640 one super final test and we'll use that for the subject line. 116 00:07:33,690 --> 00:07:37,000 The email body and this time for the recipient list. 117 00:07:37,020 --> 00:07:41,910 I'm going to put in a couple of different recipients just so we can test and make sure that we are picking 118 00:07:41,910 --> 00:07:46,860 out the correct recipient when we are doing that responded check thing and just to make sure that the 119 00:07:46,860 --> 00:07:48,770 query is working the way we expect. 120 00:07:48,770 --> 00:07:50,650 So I get it put in a few emails. 121 00:07:51,150 --> 00:07:57,760 I'll put in my personal e-mail and remember this has to be comma separated list right here. 122 00:07:57,930 --> 00:08:01,880 So put in my email I'll put in my dev. 123 00:08:01,890 --> 00:08:04,200 E-mail me at gmail dot com. 124 00:08:04,590 --> 00:08:09,030 I'll put you at the comma again and I'll put in two other junk emails. 125 00:08:09,030 --> 00:08:11,850 It doesn't matter if the e-mails don't actually successfully get sent. 126 00:08:11,850 --> 00:08:18,480 I just want to make sure that I can leave feedback with dead e-mail me even though or when there are 127 00:08:18,870 --> 00:08:21,180 multiple e-mails included in the survey. 128 00:08:21,550 --> 00:08:23,230 OK so let's send this thing out. 129 00:08:23,230 --> 00:08:28,020 I'll click Next I'll send the survey to go to my e-mail account now. 130 00:08:29,130 --> 00:08:31,060 Here's super final test. 131 00:08:31,140 --> 00:08:34,630 And just to make sure that the query is really working the way we expect. 132 00:08:34,740 --> 00:08:39,030 I'm going to click on this button a couple of times and you'll see all these tabs up here popping up 133 00:08:39,570 --> 00:08:44,160 so I'm clicking on it making sure that we're just voting a whole bunch here but we're still going to 134 00:08:44,160 --> 00:08:49,050 expect to only see one actual vote appear inside of our database. 135 00:08:49,170 --> 00:08:54,120 Now at this point you'll recall that we don't really have any console logs inside of a handler so we're 136 00:08:54,120 --> 00:09:00,270 not going to get any feedback over here saying oh looks like the query came in successful or anything 137 00:09:00,270 --> 00:09:05,220 like that so we're really just going to have to check our database to make sure that the query updated 138 00:09:05,220 --> 00:09:06,610 everything appropriately. 139 00:09:06,810 --> 00:09:07,990 So let's take a quick break. 140 00:09:08,020 --> 00:09:13,150 You know give it like a minute or two for everything to actually go over to Mongo and update for us. 141 00:09:13,290 --> 00:09:17,730 You know essentially Reece received some data from the web book so we'll give it just a quick break. 142 00:09:17,730 --> 00:09:22,140 We'll continue in the next section and we'll verify that our data is actually being updated inside of 143 00:09:22,140 --> 00:09:22,860 Mongo. 144 00:09:22,940 --> 00:09:24,700 So I'll see you in just a minute.