In this video it's time for you to do the same thing we just did to geo code.
But for fetching the forecast.
Now of course there are challenged comments.
I'm going to walk you through exactly what I'd like you to do although it's going to be very similar
to what we just did.
Now before I even pull those up we can remove the old commented out code where we were making our geo
code request that's no longer necessary as we have a replacement for that over in the geo code file.
Now I am going to still leave the commented out forecast code in place as there's no need to re go through
the logic in our head.
We can essentially copy and paste that stuff into the new function.
We're gonna end up creating.
Now it's time to talk about what I'd like you to do and I do have comments for this challenge.
Now I'm going to include an example function call as well so I'll show you where you can find all of
that on the web.
Once again it is over at Lynx dot Mead dot I O forward slash.
This one is forward slash forecast when you visit that it is going to bring you over to a set of challenge
comments as well as an example function call to make sure we're all working towards the same goal.
I'm gonna take the contents of this file.
I'm gonna bring them over to app dot J S and I'll paste them down at the bottom below the call to geo
code if you'd like.
You could also comment out the call to geo code.
So you're not seeing so much output in the terminal.
Now let's go ahead and talk about what I'd like you to do.
The goal here is to create a reusable function for getting the forecast much like we created a reusable
function for geo coding a given address.
Now you're going to create this function forecast in a separate file.
You can create it in the forecast dot J S file which you'll make in the utilize directory.
Once you have the basics set up once you have the function defined and exported you can require it in
app dot J S and call it as shown below.
So this is a sample call you can use as you're building out the function.
It's nice to know what we're working towards in terms of the arguments expected.
Now step three once you have that basic structure in place it's time to fill out the function.
In the end of the day you're going to use the same conditional logic we used up above.
If there's an error we're gonna do one thing.
If there is an error property on the body we're gonna do something else.
Otherwise we will go ahead and generate that forecast strength down below.
You're still going to do those same three things right here for each of those.
You're going to call the callback function exactly like we did for geo code where we have three separate
distinct calls to callback depending on what exactly happened when the request was made.
So over here for low level errors pass a string for the error just like we did with GEO code we got
a string argument back and the same thing is gonna be true for coordinate errors.
You can always test that by converting one of these to a string and adding a letter inside of it.
Last up the success case in this case I would expect that our callback gets called with not an error
but with data now data should be a string matching up with the forecast string we had generated up above
where we use the summary and then we move on to say it's currently the temperature out and there's an
X percent chance of rain.
So in the end of the day the goal isn't to change the functionality at all.
The goal is to just break all of that out into this separate function that we can then use and reuse
throughout our application.
If at any point along the way you're not quite sure what to do.
Just look at the code you wrote for the geo coding example because in the end of the day both are going
to look very similar though obviously the response data and the U.R.L. are slightly different just as
a reminder if you're using the weather stack API you already have the correct stuff in place so you
shouldn't need to make any code changes that you are L you're going to copy is already the weather stack
U.R.L. and the data you're going to print will already work with the weather stack response as it did
before.
All right take some time to knock that out test your work and when you're done come back and click play.
How did you do.
Hopefully you were able to get through that without too much trouble.
As always we're going to work through these solution together now.
This was by far the most code you had to write for a challenge.
And as these challenges get harder you might have a harder time solving them and that is okay.
It is by design.
The goal is to give you more room to experiment play and learn and less guidance.
And that is going to be an important piece to the puzzle.
Though it can be frustrating at first because you'll feel like you just learned this thing and you get
it but you can't put it into action and that's OK.
That's what the challenges are here to help with.
So let's go ahead and get started.
I want to create that function in a new file.
Export it and require it over here in utilize dot J S I'm going to create the forecast J S file.
Let's go ahead and start by not misspelling things then we're gonna go ahead and do what we need to
do which is to create a forecast function.
So right here forecast equals a brand new function.
We'll worry about the arguments in just a moment and once we have this function in place what are we
going to do.
We are going to export it.
So down below module dot exports.
Exporting the forecast function.
Now we can go ahead and require this file to get the function in app dot J.
S that's gonna make our function call actually work up above.
Let's go ahead and get that done const forecast equals I'll be using require to require one of my own
files.
So dot forward slash in the utilize directory.
Forward slash the file name.
Forecast leaving off the J.S. extension.
So now we actually have access to that function which means that when I try to call it at least things
won't crash and burn.
So that is step one and step two.
We have the basic functions set up.
We've loaded it in and we have our example call down below.
From here we actually need to fill out that function and let's start with its arguments.
Now as I showed you below we have the latitude.
Then we have the longitude.
Then we have the callback function.
So we have to setup all three as arguments for forecast right here latitude.
Next up would be longitude.
And then finally the callback which once again you could have called anything at all.
These names are particularly important.
You could pick anything that makes sense in my opinion though these make the most sense down below.
It's time to start the process of making that request.
Now if we are going to use the request module we should probably load it in right here.
Const request equals.
I'll use require to load in the request module and once we have it loaded in we're going to actually
use it.
Now we do need the U.R.L. and we already have that you are L an aspect J s so you could have copy and
pasted this over up above.
I'm going to grab the U.R.L. I'll grab everything inside of the quotes I'm going to copy it to the clipboard
scroll back down and bring it over to the forecast file right here.
I will also create a concept you are El variable I'm going to paste the value in and now we need to
swap out thirty seven point eight to six seven with whatever value we have for latitude and we want
to swap out the minus 1 1 2 with whatever value is passed in for the longitude right here.
Let's go ahead and get that done.
Remember we need a comma in between.
So I'm gonna remove the LAT end the long and the comma ending with that forward slash.
Then I will concatenate on the latitude by referencing that variable after the plus operator and then
I'm gonna concatenate on a comma.
Then finally Elkin can it on the longitude.
Perfect it's Andrew with another weather stack update.
So up top that we have the old the dark sky U.R.L. with those variables in place down below we have
what you should have which is the weather stack U.R.L. with the latitude and longitude variables in
place.
So we set up the base U.R.L..
Then we add the latitude from there we add our comma followed by the longitude.
And finally we wrap things up by setting up our units.
So make sure you have that bottom line in place to successfully set things up now this is going to be
the complete U R L with dynamic coordinates inside.
We can now actually fire off the request to get the response which would be the forecast information
for this particular location.
I'm gonna kick things off by calling request like we have done plenty of times before with those two
arguments.
Our options object being the first one and the callback function being the second one.
Now we can provide the correct options for the options object we have U.R.L. which is going to come
from the.
You are all variable and we have Jason the value true.
Perfect.
Next up we have the callback function itself.
This is for request and we know that it gets called with an error if any and a response.
If there is no error now we can actually set up the if statement much like we had before.
So we'll start by checking if there are any low level errors by looking for the existence of the error
object.
Next up we'll use.
Else if to check if the coordinates are bad right here to do that we're gonna look at a response dot
body dot error and that is exactly what we did before an app that J.
S the old code used that condition from there.
We have to set up some code to run if the coordinates are bad and last but definitely not least.
Else this is the code we run.
If everything went well and there's a forecast to provide now let's go ahead and actually call the callback
function which currently is not being used.
So if there is a low level error what are we going to choose to do.
We are going to go ahead and call the callback with the first argument the error and no value for the
second argument which is data.
So we want to provide a value here and we don't want to provide a value here.
Let's go ahead and get that done.
I'm gonna call a callback passing in a string for the first argument and undefined for the second.
Now once again you can leave the second argument off completely as undefined as the default value for
an on provided argument anyways.
Now we can pick our message I'm going to use the message we used before unable to connect to weather
service.
Perfect.
Next up if there's a problem with the coordinates we are once again gonna call callback.
Now things went poorly so we provide a value for the first argument and we leave off a value for the
second argument.
We can pick whatever message we like for this one I'll use the same one we had before.
Unable to find location now inside of else.
This is where things went well so we want to leave off the first argument to call back for that we are
gonna have to provide undefined.
Then as the second argument we can provide our forecast string.
Now we already have that string created in app dot J.
S Up above.
It starts right here in console dialog where we use response to get the summary.
It goes all the way to the end where we close the single quote right here.
I'm going to shift click to highlight everything in between and I'm just going to take that I'm going
to copy it to the clipboard scroll back down to the call and over in forecast dot J S we can paste it
right here between the comma and the closing parentheses.
I'm gonna paste it in and there we go.
We now have our four cached string getting sent back correctly.
Now the function is done and we already have a call in place so we can go ahead and file things off
in their current state to get the forecast.
This time it should not produce an error.
I'm gonna run node app dot J S and that is exactly what we end up getting.
Right here we have the data defined we have our summary with our temperature and percent chance of rain
and up above.
We have undefined for the error.
This is just a reminder that since you're using the weather stack API you'd see a similar forecast to
what you saw before it would start off with a description of the weather followed by the actual temperature
followed by the apparent feels like temperature.
Now let's go ahead and make sure that the error message shows up when it should.
The first thing I'm going to do is just do something like shut down my internet connection.
Right here I'll turn off Wi-Fi and then going to rerun the script and I do indeed get unable to connect
it to weather service which is great.
Let's go ahead and test that middle case by providing a bad coordinate value up above.
I'm going to bring myself back online and then I'm going to update either one or the other.
I'm gonna make this first value a string and all I'm gonna do is put a letter inside which is indeed
an invalid caught in a pair.
From here we can go ahead and rerun the script down below.
I'm gonna run things and what do I get.
I get unable to find a location which is fantastic.
Now we can remove the challenge comments and we can see how much easier our code got to use right here.
I have a call to geo code right here I have a call to forecast.
I could easily take these four lines move them into the callback and then do one thing before the next.
That is exactly what we're going to end up doing in the next video.
Now that we have these two functions in place it's gonna be easy to use them together and it's gonna
be easy to reuse them throughout our code should we need to.
All right I'm gonna wrap this video up by undoing that change I made I'll remove the letter A and remove
the quotes.
I'm gonna save the file and that's where we're gonna stop for this one up above.
Before we go we can actually also remove the old commented out forecast request leaving ourselves with
just 13 lines of code an app dot J S we can actually make that twelve because request actually isn't
being used in this file anymore which means we can remove that first line entirely bringing us down
to twelve.
All right I'll see you in the next one.