Skip to content

Instantly share code, notes, and snippets.

@auchomage
Last active July 24, 2016 13:31
Show Gist options
  • Save auchomage/4aa89a2ddf989af2825a5f8f03b83afa to your computer and use it in GitHub Desktop.
Save auchomage/4aa89a2ddf989af2825a5f8f03b83afa to your computer and use it in GitHub Desktop.
callbacksModelAnswerLearning1
// dealing with the Location
var location = require('./learningLocation.js');
// dealing with the Weather
var weather = require('./learningWeather.js');
// test the script.
// this works!!
location(function(userLocation){
console.log('Location is: ', userLocation);
});
// test the script
// this doesn't work!!!
weather(location,function(weatherData){
console.log('Weather is: ', weatherData);
});
// learningLocation
// NB: Location is not dependent on any user input
// Location is obtained from the IP address
module.exports = function(callback){
// define variables
var url = "http://ipinfo.io";
var request = require('request');
// Notification
console.log('Obtaining the location...');
// Logic
request({
url: url,
json: true
}, function(error, response, body){
if(error){
callback('');
} else {
callback(JSON.stringify(body, null, 4));
}
});
};
module.exports = function(location, callback){
// define variables
var request = require('request');
var encodedLocation = encodeURIComponent(location);
var url = 'http://api.openweathermap.org/data/2.5/weather?q=' + encodedLocation + '&appid=a0405ca935c126bca3f178e0230f75b5&units=metric';
// Check if no location is provided
if (!location){
return callback('No location provided'); // ie end the program here
}
// Notify the user
console.log('Fetching the weather data...');
// Apply logic
request({
url: url,
json: true
},function(error, response, body){
if(error){
callback('Unable to fetch the weather data!');
} else {
//console.log(JSON.stringify(body,null, 4));
callback('In city: ' + body.name + ' the temperature is: ' + body.main.temp + ' deg C.');
}
});
};
// define variables
var request = require('request');
var encodedLocation = encodeURIComponent('Sutton');
var url = 'http://api.openweathermap.org/data/2.5/weather?q=' + encodedLocation + '&appid=a0405ca935c126bca3f178e0230f75b5&units=metric';
// Notification.
console.log('LearningWeatherExpt1: Getting weather data...');
// Apply logic
request({
url : url,
json: true
}, function(error, response, body){
if(error){
console.log('Unable to fetch weather data!!')
} else {
console.log(JSON.stringify(body, null, 4));
console.log("Town: " , body.name + ': Country ' + body.sys.country + ': Temperature: ' + body.main.temp + ' deg C.');
}
});
@andrewjmead
Copy link

andrewjmead commented Jul 24, 2016

Hey,

The best way to debug this would be to following the execution of the application. On line 14 of appLearningMA.js the problem is not with the function that's called. The problem is with that first argument, location.

On line 14 the location function is getting passed in, not a string location like userLocation. That's why the program is crashing.

- Andrew

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment