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

benjaminmosery
benjaminmosery
5,403 Points

"This Key does not exist"

After running the following code in node.js I am getting the error "error": { "type": "keynotfound" ,"description": "this key does not exist".

Here is the main body of my application:

// Problem: We need a simple way to look at the weather in a certain city
// Solution: Use Node.js to connect to Wunderground's Weather API to get weather information to print out

const https = require ("https"); 
const api = require (`./api.json`);
//Function to print Message to Console 
function Printmessage (weather) {
    const message = `Current temperature in ${weather.location.city} is ${weather.current.observation.temp_f} F.`;
console.log(message)
}
//Connect to the API URL (https://api.wunderground.com/api.json)
function get (query) {
const GetWeather = https.get (`https://api.wunderground.com/api/${api.key}/geolookup/conditions/q/${query}.json`, response  =>  {
//Read the Data in
let body = ``;
response.on('data', chunk => {  
//GETTING THE BODY BY WRITING IN THE WEBSITE RESPONSE
    body+= chunk; //HAVING THE DATA APPEAR IN THE CONSOLE
  });
response.on(`end`,() => {
console.log(body);
});
//Parse the .json Data (as its in a string)
//Print the Data

}); 
}
module.exports.get = get;

Why am I getting this error? My api key is valid according to the wunderground site and is stored in a seperate JSON file. Any help would be appreciated.

2 Answers

Carlos Lantigua
Carlos Lantigua
5,935 Points

it my help to take out the http(s).. the intructor video shows that he used the http(s) but it specifically says on the site to only use http. once I did this on mine it got rid of my errors. Also, if this does not work you might need to take steps to get a replacement key or copy paste the key directly from the API site. Hope this helps.

Adam Lyles Kauffman
Adam Lyles Kauffman
11,325 Points

the api service provided in your code no longer offers free api keys to the public. there is a new service openweathermap.org which will give you a key after registering for an account. use the updated code provided in the teachers notes for both files, also update the key value in your api.json. after parsing the new data from the new weather api service stored in the varible body, you can now access new objects using dot notation. for example my message ended up being const msg = Current tempature in ${weather.sys.name} is ${weather.main.temp} with the max tempature being ${weather.main.temp_max} and the minimum being ${weather.main.temp_min};