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

jbiscornet
jbiscornet
22,547 Points

Need help getting data from API

I don't know what I'm doing wrong but I'm trying to get a little basic info from a weather API. I can get the full JSON parsed list from the API, but when I try to refer to basic info in it and put it into my template literal, it comes up in the console as undefined. I just get the message printed with the entire message shown in the console just as I would see it in my code (not the info it is supposed to refer to). I've been over the object notation and I'm pretty sure that is correct and I have also tried many other inputs but nothing changes so I am running out of options to try, any help would be greatly appreciated.

https://w.trhou.se/899mfwafas

nico dev
nico dev
20,364 Points

Hi J Biz,

Did you already receive (and included in the url) your API Key? There are a LOT of reports in their forum about people who simply didn't ever receive it, like me. :)

I tried the other app he mentioned, however, the openweathermap and it worked wonderfully. Hop this helps in your specific case.

Steven Parker
Steven Parker
231,007 Points

When posting code, usethe instructions for code formatting in the Markdown Cheatsheet pop-up below the "Add an Answer" area. :arrow_heading_down:   Or watch this video on code formatting.

Even better, if your project is in a workspace, you can make a snapshot of your workspace and post the link to it here. And you can upload screenshots to a workspace folder before making the snapshot.

2 Answers

nico dev
nico dev
20,364 Points

OK, J Biz , I don't blame you at all on this one. :)

I found two issues here. One is little and easy to fix, and it's not a big deal, now the second one is 100% not your fault. The thing is the API changed since this course was made. (That's how fast technology goes! Imagine if we're actually working with the API, we'd have had to run after a solution).

The first thing I found is in your message constant. The template literal is alright, the only problem, though, is that just as you included current_observation for the weather, the same applies to display_location, since they're both inside the current_observation object.

// So this:
const message = `The weather in ${weather.display_location.full} today is ${weather.current_observation.weather}`;
// (This would tell you "Hey! I can't read full, because it belongs to weather.display_location, which is undefined, in other words: I don't find it".)

// should just be:
const message = `The weather in ${weather.current_observation.display_location.full} today is ${weather.current_observation.weather}`;

The second one is a little more tricky. If you check their docs, they show now there that the query now requires the state or country -slash- the city dot json. Like this:

https://api.wunderground.com/api/Your_Key/conditions/q/CA/San_Francisco.json
//or
http://api.wunderground.com/api/Your_Key/conditions/q/France/Paris.json

There was someone who already commented about this issue here, and honestly the answer there deserves an Oscar, a Pulitzer or a Grammy (you choose!), still, I found a similar solution that I'd like to share with you. NOTE: Anyway, it's clear that you'll have to help yourself with some Front-End-UI-ish stuff to make sure to get the right input (if it's for an end user), or find more specific and ellaborate solutions (if this will only interact with a system), and also, of course, take care of many more scenarios.

Here it is:

// Replace:
// const weatherSearch = process.argv.slice(2).join("_").replace(" ", "_");
// with:
const city = process.argv.slice(2, -1).join("_").replace(" ", "_");
const state = process.argv.slice(-1)[0];
const location = `${state}/${city}`;

// And then, obviously, change your get() url to:
const request = https.get(`https://api.wunderground.com/api/${api.key}/conditions/q/${location}.json`, response => {
// rest of the code here, blah, blah...
}

This way it worked with me, but of course it's still very raw and it can (and should!) be improved a lot, both in modularity (if that's how you call it, notice I didn't make any extra exports to your work, etc.), and in functionality. So you still have plenty of work to do. :) Hope at least that helps you go on with it. Let me know if it worked to you too!

jbiscornet
jbiscornet
22,547 Points

Thanks so much for that Nico you are awesome. I spent so much time going over that. I missed that object within the object, I see it now. That simple change made all the difference. It works well for me without changing the state/ city. I just put in the "_" between city and state or even just a zip and it works well.