0

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... :'(

H Skee
  • 49
  • 2
  • 10
  • 1
    looks like you're missing the third param: `weather(lat, long, yourCallbackFunction);` – mika Oct 20 '16 at 23:06

2 Answers2

0
var weather = (lat, long, callback) => {

Declared with three parameters

weather(lat, long);

Called with two.

Had you added a debugging line like:

var weather = (lat, long, callback) => {
  console.log('callback = ', callback);

You could have seen the issue that callback is, indeed, undefined.

Jeremy J Starcher
  • 23,369
  • 6
  • 54
  • 74
0

Your call has to use a function as a callback, which it indeed does not. Call it like this:

weather (lat, long, function (celsius) {
    alert (celsius);
});

Note that this is now asynchronous. For more information, check What is the difference between synchronous and asynchronous programming (in node.js)

Community
  • 1
  • 1
NikxDa
  • 4,137
  • 1
  • 26
  • 48