-
Notifications
You must be signed in to change notification settings - Fork 0
/
get-forecast.js
28 lines (26 loc) · 1.15 KB
/
get-forecast.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
const axios = require('axios');
var getForecast = (address) => {
var geocodeUrl = `https://maps.googleapis.com/maps/api/geocode/json?address=${address}&key=AIzaSyAGiRK6e3U3AMkePm4wJrhq7Jizh9j3vhs`;
axios.get(geocodeUrl)
.then((response) => {
if(response.data.status=== 'ZERO_RESULTS' || response.data.status=== 'INVALID_REQUEST') {
throw new Error('Unable to find address');
}
var forecastUrl = `https://api.darksky.net/forecast/665f7498ad8965682001332b863e5b2a/${response.data.results[0].geometry.location.lat},${response.data.results[0].geometry.location.lng}?units=si`;
console.log(response.data.results[0].formatted_address);
return axios.get(forecastUrl);
})
.then((response) => {
console.log(`The temperature is ${response.data.currently.temperature}. Feels like ${response.data.currently.apparentTemperature}. ${response.data.currently.summary}`);
})
.catch((error) => {
if(error.code === 'ENOTFOUND') {
console.log('Cannot conect to server.');
} else {
console.log(error.message);
}
});
}
module.exports = {
getForecast
}