In our arguments definition, instead of just expecting an address argument we'll also expect a callback argument, and we can call this callback argument whenever we like. We'll call it in three places. We'll call it once inside of the if (error) block, instead of calling console.log we'll simply call the callback with the Unable to connect to Google servers. string. This string will be the error message we defined in geocodeAddress function in app.js.
In order to do this, all we need to do is change our console.log call to a callback call. We'll pass it as the first argument our error message. We can take the string exactly as it appeared in console.log, and move it into the arguments for callback. Then I can remove the console.log call and save the file. The resultant code will look like following:
request({
url: `https://maps.googleapis.com/maps/api/geocode/json?address=${encodedAddress}`,
json: true
}, (error, response, body) => {
if (error) {
callback('Unable to connect to Google servers.');
}
Now we can do the exact same thing in the next else if block for our other console.log statement, when there is zero results, we'll replace console.log with callback:
if (error) {
callback('Unable to connect Google servers.');
} else if (body.status === 'ZERO_RESULTS') {
callback('Unable to find that address.');
}
Now the last else if block will be a little trickier. It's a little trickier because we don't exactly have our object. We also need to create an undefined variable for the first argument, since an error message will not be provided when things go well. All we have to do to create that undefined error message is call callback, passing an undefined variable as the first argument. Then we can go ahead and specify our object as the second argument, and this object, this will be exactly what's in the geocodeAddress function, results:
} else if (body.status === 'OK') {
callback(undefined, {
})
console.log(`Address: ${body.results[0].formatted_address}`);
console.log(`Latitude: ${body.results[0].geometry.location.lat}`);
console.log(`Longitude: ${body.results[0].geometry.location.lng}`);
}
Now as I mentioned the results have three properties: the first one will be formatted address, so let's go ahead and knock that out first. We'll set address equal to body.results, just like we have in the Address variable of console.log statement:
} else if (body.status === 'OK') {
callback(undefined, {
address: body.results[0].formatted_address
})
console.log(`Address: ${body.results[0].formatted_address}`);
console.log(`Latitude: ${body.results[0].geometry.location.lat}`);
console.log(`Longitude: ${body.results[0].geometry.location.lng}`);
}
Here we're making things even easier, instead of having complex properties that are nested deep inside of an object inside of app.js, we'll be able to access a simple address property, and we'll do the same thing for Latitude and Longitude of console.log statements.
Next, we'll grab the code that let us fetch the latitude, and I'll add my second property, latitude, setting it equal to the code we grab from the console.log statement. Then we can go ahead and add the last property, which will be longitude, setting that equal to the latitude code, replacing lat with lng. Now that we have this in place we can add a semicolon at the end, and remove the console.log statements since they're no longer necessary, and with this we are done:
if (error) {
callback('Unable to connect Google servers.');
} else if (body.status === 'ZERO_RESULTS') {
callback('Unable to find that address.');
} else if (body.status === 'OK') {
callback(undefined, {
address: body.results[0].formatted_address,
latitude: body.results[0].geometry.location.lat,
longitude: body.results[0].geometry.location.lng
});
}
We can now rerun the file, and when we do we'll pass an address to geocodeAddress, this will go off and make the request, and when the request comes back, we'll be able to handle that response in a really simple way.