Last active
July 24, 2016 13:31
-
-
Save auchomage/4aa89a2ddf989af2825a5f8f03b83afa to your computer and use it in GitHub Desktop.
callbacksModelAnswerLearning1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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)); | |
} | |
}); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.'); | |
} | |
}); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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.'); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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