I am kinda new to Node JS, so I started with something simple. A weather app. But I have a problem with it, I'm searching for hours now and I can't find a solution. So thats why I'm asking here.
I get an error with my callback. This is my code:
var weather = (lat, long, callback) => {
request({
url: `https://api.darksky.net/forecast/79edcc21428e0d3076f214db1935daeb/${lat},${long}`,
json: true
}, (error, response, body) => {
var fahrenheit = body.currently.temperature;
var celsius = Math.round(((fahrenheit-32)/1.8) * 10) /10;
console.log(fahrenheit);
console.log(celsius);
callback(celsius);
});
};
And I call the callback here:
// stuff to get latitude and longtitude
weather(lat, long);
var test = celsius;
That variable might be wrong but I get the error also without it.
I hope someone can help me out, because I just don't know how to solve it :(
For those who want to see the full code: (I'm dutch so the messages are in dutch too, but it's nothing special.)
const request = require('request');
var weather = (lat, long, callback) => {
request({
url: `https://api.darksky.net/forecast/79edcc21428e0d3076f214db1935daeb/${lat},${long}`,
json: true
}, (error, response, body) => {
var fahrenheit = body.currently.temperature;
var celsius = Math.round(((fahrenheit-32)/1.8) * 10) /10;
console.log(fahrenheit);
console.log(celsius);
callback(celsius);
});
};
var address = (address, callback1, celsius) => {
var address = encodeURIComponent(address);
request({
url: `https://maps.googleapis.com/maps/api/geocode/json?address=${address}`,
json: true
}, (error, response, body) => {
if (error) {
callback1('Niet mogelijk om de Google servers te contacteren.');
} else if(body.status === 'ZERO_RESULTS') {
callback1('Niet mogelijk om dat address te vinden.');
} else if(body.status === 'OK') {
var lat = body.results[0].geometry.viewport.northeast.lat;
var long = body.results[0].geometry.viewport.northeast.lng;
weather(lat, long);
var test2 = celsius;
callback1(undefined, {
Test: `test: ${test2}`,
Address: body.results[0].formatted_address,
// Lat: body.results[0].geometry.viewport.northeast.lat,
// Long: body.results[0].geometry.viewport.northeast.lng
});
} else {
callback1('Er is iets mis gegaan.');
}
});
};
module.exports.address = address;
EDIT: The weird thing is that it doesn't work in the weather variable, while it does work in the address variable... :'(