JavaScript Node.js Basics Create a Command Line Weather Application Parsing Data and Printing - Solution

Brian Patterson
Brian Patterson
19,586 Points

Getting an error from his code

I have gone through his code and I am getting the following error.

<!DOCTYPE html>

SyntaxError: Unexpected token < in JSON at position 1
    at Object.parse (native)
    at IncomingMessage.response.on (/Users/briankaty1/Dropbox/JavaScript/consoleWeatherApp/weather.js:24:32)
    at emitNone (events.js:91:20)
    at IncomingMessage.emit (events.js:185:7)
    at endReadableNT (_stream_readable.js:974:12)
    at _combinedTickCallback (internal/process/next_tick.js:74:11)
    at process._tickCallback (internal/process/next_tick.js:98:9)

This is the code I have for teh weather.js file.

const https = require('https');

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

//const city = "London";

function printWeather(weather) {
  const message = `Current temperature in ${} is

//connecting to openweather API.
//' + city + "&units=metric" + "&appid=8a99df7bf7ef4a4a202732c392b2d240"
function get(query) {
  const request = https.get(`${api.key}/geolookup/conditions/q/CA/${query}.json`, response => {
    let body = "";
    response.on('data', chunk => {
      body += chunk;
    response.on('end', () => {
      //parse the data
      const weatherData = JSON.parse(body);

    //print the data.

module.exports.get = get;

and this is the code I have for app.js file.

const weather = require('./weather');

const query = process.argv.slice(2).join("_").replace(' ', '_');


Not sure what I have done wrong.

1 Answer

Steven Parker
Steven Parker
194,522 Points

I see two issues: first, the request URL is not properly constructed here:


It's missing the term "/api/" right before the key, and unless you only want to look up cities in California, it should not have the term "/CA/" between the "q" and the query. A correct and more typical URL construction would be:


Then when the data is parsed it is assigned to the constant "weatherData", but when the "printWeather" function is called on the next line, it is given the argument "weather" (which is undefined).