Before we go ahead and make any changes in geocode.js, we want to take a look at how we'll structure things inside of app.js. We'll pass an arrow function to geocodeAddress, and this will get called after the request comes back:
geocode.geocodeAddress(argv.address, () => {
});
In the parentheses, we'll expect two arguments, errorMessage, which will be a string, and results, which will contain the address, the latitude, and the longitude:
geocode.geocodeAddress(argv.address, (errorMessage, results) => {
});
Out of these two only one will be available at a time. If we have an error message we'll not have results, and if we have results we'll not have an error message. This will make the logic in the arrow function, of determining whether or not the call succeeded, much simpler. We'll be able to use an if statement, if (errorMessage), and if there is an error message, we can simply print it to the screen using console.log statement:
geocode.geocodeAddress(argv.address, (errorMessage, results) => {
if (errorMessage) {
console.log(errorMessage);
}
});
There's no need to dig into any sort of object and figure out exactly what's going on, all of that logic is abstracted in geocode.js. Now if there is no error message inside of the else clause, we can go ahead and print the results. We'll use that pretty print method we talked about in the previous chapter, we'll add the console.log(JSON.stringify) statement, and we'll pretty print the results object which will be an object containing an address property, a latitude property, and a longitude property.
Then, we'll pass the undefined argument as our second argument. This skips over the filtering function which we don't need, and then we can specify the spacing, which will format this in a really nice way, we'll use two spaces as shown here:
geocode.geocodeAddress(argv.address, (errorMessage, results) => {
if (errorMessage) {
console.log(errorMessage);
} else {
console.log(JSON.stringify(results, undefined, 2));
}
});
Now that we have our function set up inside of geocodeAddress function in app.js, and we have a good idea about how it will look, we can go ahead and implement it inside of geocode.js.