Struggling with the wunderground api and node

90 views Asked by At

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

0

There are 0 answers