Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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

Mary Paul
Mary Paul
13,792 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,314 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,792 Points

still getting the same error.