Dave Hock (2) [Avatar] Offline
#1
Hello, I am hoping someone can help with a question concerning section 5.6. When i ran the application I was getting this error > 'APIKey must be set on Forecast options' so after a little research I added the following to the app,js file.

var DarkSky = require('forecast.io');
const DARKSKY_API_KEY = '883674b78b8d36c1f0fafced6afc2206';

var app = express();

var options = 
{
    APIKey: DARKSKY_API_KEY,
    timeout: 1000
};

var weather = new DarkSky(options);


This code above replaced the following line:
var weather = new DarkSky(DARKSKY_API_KEY);


I am no longer receiving the error about the API Key but I am recieving an error in the following code:
app.get(/^\/(\d{5})$/, function(req, res, next)
{
    var zipcode = req.params[0];
    var location = zipdb.zipcode(zipcode);
    if (!location.zipcode)
    {
        next();
        return;
    }

    var latitude = location.latitude;
    var longitude = location.longitude;

    weather.get(latitude, longitude, function(err, data)
    {
        if (err)
        {
            next();
            return;
        }

        res.json(
            {
                zipcode: zipcode,
                temperature: data.currently.temperature
            });
    });
});


The error I am receiving concerns the temperature parameter in the res.json section.

TypeError: Cannot read property 'temperature' of undefined at C:\WebProjects_Express\TempByZipcode\app.js:54:45

Can someone please review and let me know if I am missing something or if you need additional information.


Thanks very much,


Dave Hock
Dave Hock (2) [Avatar] Offline
#2
Hello, this is an update to this issue. I found a solution.

updates to original code:
line 18: added response parameter
line 26: added variable results
line 31: changed data to results

app.get(/^\/(\d{5})$/, function(req, res, next)
{
    var zipcode = req.params[0];
    var location = zipdb.zipcode(zipcode);
    
    if (!location.zipcode)
    {
        next();
        return;
    }

    var latitude = location.latitude;
    var longitude = location.longitude;

    console.log(latitude);
    console.log(longitude);

    weather.get(latitude, longitude, function(err, response, data)
    {
        if (err)
        {
            next();
            return;
        }

        var results = JSON.parse(response.body);
        
        res.json(
            {
                zipcode: zipcode,
                temperature: results.currently.temperature
            });
    });
});
Evan Hahn (70) [Avatar] Offline
#3
Glad you found a solution! Let me know if you run into any other issues.
550725 (2) [Avatar] Offline
#4
I think i want to put forth a personal challenge to myself and go through this exercise line by line, file by file, and further document how the different lines of code work - what each command accomplishes and how they come together - I think it will be a great exercise, being that it's halfway through the book -- Stay tuned