To change our console.log into callback calls, for the first two console.log calls we can replace console.log with callback. And this will line up with the arguments that we specified in app.js, where the first one is the errorMessage and the second one is the weatherResults. In this case we'll pass the errorMessage back and the second argument is undefined, which it should be. We can do the same thing for Unable to fetch weather:
if (error) {
callback('Unable to connect to Forecast.io server.');
} else if (response.statusCode === 400) {
callback('Unable to fetch weather.');
}
Now the third console.log call will be a little more complex. We'll have to actually create an object instead of just passing the temperature back. We'll call the callback with the first argument being undefined, because in this case there is no errorMessage. Instead we'll provide that weatherResults object:
if (error) {
callback('Unable to connect to Forecast.io server.');
} else if (response.statusCode === 400) {
callback('Unable to fetch weather.');
} else if (response.statusCode === 200) {
callback(undefined, {
})
console.log(body.currently.temperature);
}
Inside the parentheses, we can define all the temperature properties we like. In this case we'll define temperature, setting it equal to body.currently, which stores all of the currently weather data, .temperature:
else if (response.statusCode === 200) {
callback(undefined, {
temperature: body.currently.temperature
})
console.log(body.currently.temperature);
}
Now that we have the temperature variable we can go ahead and provide that second property to the object, which is actual temperature. Actual temperature will account for things like humidity, wind speed, and other weather conditions. The actual temperature data is available under a property on currently called apparentTemperature. We'll provide that. And as the value we'll use the same thing. This gets us to the currently object, just like we do for temperature. This will be body.currently.apparentTemperature:
else if (response.statusCode === 200) {
callback(undefined, {
temperature: body.currently.temperature,
apparentTemperature: body.currently.apparentTemperature
})
console.log(body.currently.temperature);
}
Now we have our two properties, so we can go ahead and remove that console.log statement. Add a semicolon. The final code will look like:
const request = require('request');
var getWeather = (lat, lng, callback) => {
request({
url: `https://api.forecast.io/forecast/4a04d1c42fd9d32c97a2c291a32d5e2d/${lat},${lng}`,
json: true
}, (error, response, body) => {
if (error) {
callback('Unable to connect to Forecast.io server.');
} else if (response.statusCode === 400) {
callback('Unable to fetch weather.');
} else if (response.statusCode === 200) {
callback(undefined, {
temperature: body.currently.temperature,
apparentTemperature: body.currently.apparentTemperature
});
}
});
};
module.exports.getWeather = getWeather;
Now we can go ahead and run the app. We have our getWeather function wired up both inside of the weather.js file and inside of app.js. Now once again we are still using static coordinates, but this will be the last time we run the file with that static data. From the Terminal we'll rerun the application:

And as shown we get our temperature object printing to the screen. We have our temperature property 48.82 and we have the apparentTemperature, which is already at 47.42 degrees.
With this in place we're now ready to learn how to chain our callbacks together. That means in app.js we'll take the results that come back from geocodeAddress, pass them in to getWeather, and use that to print dynamic weather for the address you provide over here in the Terminal. In this case we would get the address for the town in New Jersey. As opposed to the static address which we're using in the app.js file that latitude/longitude pair is for Philadelphia.