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

Nikola Jankovic
Nikola Jankovic
10,793 Points

console.dir(core); trows an error

const http= require("http");
const query="london";
const api= require('./api.json');

function printMessage(city, temperature , airPresure) {
  const string= `In ${city} temperature is ${temperature} and air resure is ${airPresure}`;
  console.log(string);
};

function get(query) {
  const request= http.get(`http://api.openweathermap.org/data/2.5/forecast?q=${query}&APPID=${api.key}`, response=> {
    let body= "";
    response.on("data", data=>{
      body+= data.toString;
    });
    response.on('end', ()=>{
      const core= JSON.parse(body);
      console.dir(core);
    });
  });
};

get(query);

This is the error message in the console=> Unexpected token u in JSON at position 1

1 Answer

Jordan Watson
Jordan Watson
14,738 Points

Within the function get() you have a variable

 let body = ""

This is within a function call this is now scoped within that please see comments!

function get(query) {
  let body= ""; // Move this to the top of the function! 
  const request= http.get(`http://api.openweathermap.org/data/2.5/forecast?q=${query}&APPID=${api.key}`, response=> {
    // let body= ""; <--- This is scoped within this function ^
    response.on("data", data=>{
      body+= data.toString;
    });
    response.on('end', ()=>{
      const core= JSON.parse(body);
      console.dir(core);
    });
  });
};