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 Parsing Data and Printing - Solution

Łukasz Sitnik
Łukasz Sitnik
3,306 Points

It seems I have no access to the API's properties

Dear Support Team,

When I am testing my code from "Parsing Data and Printing - Solution" in Node.js Basics course I get such error "TypeError: Cannot read property 'city' of undefined", what means this part of code ${weather.location.city} cannot be read. It seems I haven't gained the access to the Weather API, but I don't know what went wrong. I probably did not understand something well and cannot myself get the idea where is my mistake. Please, help.

Best Regards, Lukasz Sitnik

9 Answers

Steven Parker
Steven Parker
231,261 Points

Account credentials might not be the same as an API key. And I would not expect the API key from one service to work on another. Since the program in your snapshot is accessing wunderground it most likely requires a key obtained directly from them (such as one from the link I provided) and will not work with a key from openweathermap.

I can confirm that your program does work (after that small fix) when given a key provided by wunderground. And as I said, the working key looks very different from the one currently in the snapshot.

Steven Parker
Steven Parker
231,261 Points

It sounds like you don't have anything stored in "weather".

For more specific help, you'd need to show your code. Preferably, by making a snapshot of your workspace and posting the link to it here.

Steven Parker
Steven Parker
231,261 Points

Have you replaced the API key with a real one you registered for? One obvious way to get this particular error would be to pass the API an invalid key. This is exactly what happens when I run the snapshot verbatim.

I replaced the call to "printWeather(weather)" on line 33 of weather.js with "console.log(weather);" This allowed me to see the complete response record. You might try this even if you're using a good key to see if you're being sent an error packet by the API for some other reason.

Łukasz Sitnik
Łukasz Sitnik
3,306 Points

Yes, you're right. I also get this error after testing my script in console.log, but the key is correct. I checked many times.

Steven Parker
Steven Parker
231,261 Points

So in your actual testing, you use a key other than the one in the snapshot, right?

Łukasz Sitnik
Łukasz Sitnik
3,306 Points

No, I'm changing nothing in my code. I even tried to generate a new API key and use it, but the result was the same, so I returned to the previous one. Therefore I think the problem is not in key itself, but in implementation of the query.

Steven Parker
Steven Parker
231,261 Points

That key in the snapshot is definitely not a valid key for this service. Which is perhaps a good thing, since you wouldn't want to publish a real one anyway.

Just to be sure, I subscribed and acquired a key for myself, plugged it into your program, and it ran fine after I corrected the spelling of "current_ovservation" to "current_observation" on line 8 of weather.js.

Also, my key is simply 16 hex digits. The one in the snapshot doesn't look remotely like it and is prefixed with "&APPID=". I might guess that this key is for a different service. Try getting a new key from this page.

Łukasz Sitnik
Łukasz Sitnik
3,306 Points

I was also trying to use: &APPID=465ec789b6fcac96319b0633fa52373f, but nothing changed. I am using this website to create API key: http://openweathermap.org/api. Do you think this is the couse? When I am using this: https://www.wunderground.com/weather/api/d/pricing.html (you've pasted above) also can log in with the same login and password.

Łukasz Sitnik
Łukasz Sitnik
3,306 Points

Hello again, That was the point. The problem was I used key form different website. Thank you for help.