JavaScript Node.js Basics Create a Command Line Weather Application Retrieving Data - Solution

Mary Paul
Mary Paul
13,791 Points

"Cannot read property temp_f of undefined"

Having a lot of trouble with this one but I think I'm close. I've read the API and json over and over but can't find why I'm getting this error. Can anyone see my mistake?

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(weather);
  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;

Moderator edited: Markdown added so that code renders properly in the forums. -jn

1 Answer

Adam Beer
Adam Beer
11,268 Points

Hi Mary! Please try this line. I'm not sure how this is the problem.

const message = `Current temperature in ${weather.location.city} is ${weather.current_observation.temp_f}F`;
Mary Paul
Mary Paul
13,791 Points

still getting the same error.