In the last section we finished up our update one query.
So now we're ready to take this query and move it back over to our route handler.
I'm going to copy the entire query that we just wrote so I can highlight everything and copy it and
then I'm going to change back to our survey route's file where we've been working on our web hoax route
handler.
Here it is right here.
Now I'm looking at the route handler body.
I'm trying to think of exactly where the best place is to actually write in this query.
Well remember when we iterate through the erect body object we process all these different events and
then eventually assign it to the events array.
Right here are the events variable.
So really we want to iterate over this array.
And for every processed event we want to execute that query.
So you might be thinking OK well let's write a loop down here.
So rather than doing the concert log maybe we need to loop right here over the events array.
And for every event we will issue the query.
But remember we've already wired up this fancy chain helper right here that we can use to just do one
more iteration step through the list of events.
So we don't have to assign events to this intermediate variable and then iterate through it.
We can just add on another branch to our chain right here or another step to the chain to then iterate
over the list of events that have been calculated by these three steps right here and issue the query
for each and every one.
So I'm going to find my unique by statement right underneath that.
I'm going to add in one more step called Dot each.
And so as you might imagine this dot each function will run over every single element in the events
array.
So I will say for every event we want to essentially run our query.
So inside of this or a function I'm going to paste the query that we just wrote.
So here's the whole query right here.
Now we just run off and try to test this out inside the browser.
There are still a few odds and ends that we want to take care of.
So the first thing I want to do is to make sure that I clean up the Konst events declaration up here
because we're not actually making use of these events.
We not actually cost logging them or anything like that.
All we need to do is run this entire chain statement and that's it.
So I'm not going to assign anything to the events variable anymore.
I'm going to make sure that I clean up that console log of events down here as well.
So I should have no other references to events.
I guess that's cleanup's step 1.
Now the next thing we need to do is make two small changes to the query that we just wrote the update
one query.
Both these are total gotchas that are just little odds and ends that made more sense to do right now
rather than tackle.
Over here when we are actually trying to put together the query.
So kind of gotcha number one gotcha number one right now we are looking for an ID of survey ID.
So gotcha number one is that in the Mongo D-B world internally in long ago all of our records are assigned
not an id property but an underscore id property.
And so any time that we want to specifically look for a record with a given ID by using find one or
update one or delete one or any of these different helpers we have to say underscore Id rather than
just Id like so now this is how things work in the Mongo world.
The reason that we are able to use ID in many other places inside of our codebase is because mongoose.
Remember mongooses city involved with Mago mongoose automatically will it will respect the ID property.
However whenever you are passing a query off to Mago you have to specifically say underscore dot or
underscore ID.
Now if that sounds really confusing arbitrary will it kind of is.
Let me just say that at the end of the day you can always be safe with just about all the code that
you write by just using underscore ID everywhere.
So it is sort of throw that out there.
OK now small change number 2 to our query.
So here is the opening parentheses.
Here is the closing parentheses for the query.
After we close off the query right here but before we close off the each statement.
So here is the closing curly brace and parentheses for each.
So on the query still we're going to call DOT exec like so.
So don't exact is short for Dot execute with this update one statement that we've written right here.
This just kind of puts equerry together but it doesn't actually execute it doesn't actually send it
off to the database.
So we need to make sure that we actually execute the query by calling dot exec.
Now one last thing I think inside the query you'll notice that this each function is being called with
the event object.
But we really only care about the survey ID the email and the choice properties of the event.
So rather than just plugging in the entire event we'll do a little bit of destructuring.
So will place parentheses curly braces and I will say I only care about Servais ID email and choice
like so.
OK.
Now when I save it looks like we get a nice big jump from my prettier add on.
OK.
So I think this is looking good.
Now one last thing I want to mention to you because you might be a little bit confused about one thing
that we are actually not going to do this time around this query that we are issuing right here.
This query that we are executing it is another asynchronous piece of code.
Its something that takes some amount of time to go over to the database and execute this little query.
However you'll notice that we are not going to put in any async awaits or any promises or anything like
that.
And the reason for this is that in the context of our web Hoque right here we don't really have anything
to respond back to send grid with really send grids web hook is send grid doing us a favor.
They're just saying hey here is the state.
Your ego.
And we take that data and we do some processing on it.
We don't have to respond to send grid with any specific stuff.
So really once said grid sends us our data it's up to us like we're on our own at that point.
We do not have to wait for this query to finish before we send back a response.
And in fact centigrade and could care less if we send them back some type of response.
So we don't really need any asynchronous handlers here because we don't need to put in any type of checks
to make sure that oh did everything like save.
Or is it all done or do we need to respond with the records.
No we don't care about any of that stuff.
We want to just attempt to apply these updates and that's it.
Like who cares.
That's all.
All right.
I think maybe one last thing inside this file we are making use of the survey model class.
So let's make sure that we require this survey or model class at the top of the file.
So scroll up to the top and I think that I already added in a required statement from mongoose because
I just wanted to do one little test around the query.
So make sure that you get the required statement for mongoose and then make sure that you also get the
survey model class often mongoose as well with Mongoose model surveys.
So just double check make sure you've got both those lines.
OK time to test this thing out.
So the first thing I'm going to do is flip on over to my terminal and I'm going to verify make sure
that my server is still running without any big nasty error messages.
So nothing that says like hey you got a big typo in here.
So it looks like I'm OK.
So I flip back over to my re-act application and I'm going to create a new survey so we'll call this
one super final test and we'll use that for the subject line.
The email body and this time for the recipient list.
I'm going to put in a couple of different recipients just so we can test and make sure that we are picking
out the correct recipient when we are doing that responded check thing and just to make sure that the
query is working the way we expect.
So I get it put in a few emails.
I'll put in my personal e-mail and remember this has to be comma separated list right here.
So put in my email I'll put in my dev.
E-mail me at gmail dot com.
I'll put you at the comma again and I'll put in two other junk emails.
It doesn't matter if the e-mails don't actually successfully get sent.
I just want to make sure that I can leave feedback with dead e-mail me even though or when there are
multiple e-mails included in the survey.
OK so let's send this thing out.
I'll click Next I'll send the survey to go to my e-mail account now.
Here's super final test.
And just to make sure that the query is really working the way we expect.
I'm going to click on this button a couple of times and you'll see all these tabs up here popping up
so I'm clicking on it making sure that we're just voting a whole bunch here but we're still going to
expect to only see one actual vote appear inside of our database.
Now at this point you'll recall that we don't really have any console logs inside of a handler so we're
not going to get any feedback over here saying oh looks like the query came in successful or anything
like that so we're really just going to have to check our database to make sure that the query updated
everything appropriately.
So let's take a quick break.
You know give it like a minute or two for everything to actually go over to Mongo and update for us.
You know essentially Reece received some data from the web book so we'll give it just a quick break.
We'll continue in the next section and we'll verify that our data is actually being updated inside of
Mongo.
So I'll see you in just a minute.