0

So I had been stuck on this exercise in Treehouse some time ago and just moved on. I came back to it now that I understand things better and I'm still fighting with the wunderground api. I've read through the json data and documentation, updated a few things from when the class was first recorded (and the API updated since then), and still am getting errors I can't field. I've got three js files- app.js, weather.js, and api.json (which is just my api key so not shared here.)

After my corrections, I'm still getting the error "TypeError: Cannot read property 'temp_f' of undefined" which doesn't make sense as I keep reading over the JSON to check that it's pointing to the right place.

Can anyone put an end to my misery trying to fix this?

App.js:

const weather = require('./weather');
//Join multiple values passed as arguments and replace all spaces with underscores
const query = process.argv.slice(2).join("_").replace(' ', '_');
//query: 90201
//query: Cleveland_OH
//query: London_England
weather.get(query);

Weather.js

const https = require('https');
const http = require('http');
const api = require('./api.json');

// Print out temp details
function printWeather(weather) {
  const message = `Current temp in ${weather.location} is ${weather.current_observation.temp_f}F`;
  console.log(message);
}

// Print out error message

function get(query) {
    const request = http.get(`http://api.wunderground.com/api/${api.key}/conditions/q/${query}.json`, response => {
   
      
        let body = "";
        // Read the data
        response.on('data', chunk => {
            body += chunk;
        });
        response.on('end', () => {
            //Parse data
            const weather = JSON.parse(body);
            //Print the data
            printWeather(weather);
        });
    });
}

module.exports.get = get;

//TODO: Handle any errors
zero298
  • 25,467
  • 10
  • 75
  • 100
chauxvive
  • 238
  • 4
  • 16
  • 1
    Can you print `weather` in `printWeather()` and make sure it isn't undefined? – zero298 Apr 10 '18 at 15:02
  • 1
    How are you executing your code ? It works for me if I do node app.js 90201 Clevelend_OH London_England – Vashnak Apr 10 '18 at 15:05
  • I'm executing it within the Treehouse workspace- which should be in normal node. How are you executing it? Still can't figure the darn thing out. – chauxvive Apr 27 '18 at 20:45

0 Answers0