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
Eric Conklin
8,350 PointsSet up Data to Pass to the API
Here's the question: To use the Open Weather Map API, you need to specify a city name and state or country. Add a property to the data object. The name of the property should be q and the value should be a string with the name of a city, followed by a comma, and a state or country name. For example: q : "Portland,OR"
My code is saying I am not defining a property, but as far as I can tell I am defining data.q properly using this code:
$(document).ready(function() {
var weatherAPI = 'http://api.openweathermap.org/data/2.5/weather';
var data = { };
data.q = "Portland,OR";
});
I have tried it with a : and with a [""] syntax and it never seems to allow me to create the q object within data nor does it allow me to define that object.
Any help here would be appreciated.
5 Answers
Jason Anello
Courses Plus Student 94,610 PointsHi Eric,
I don't know which challenge this is for so I can't test but did you try the : syntax within the curly braces? Maybe the challenge specifically wants you to add the property/value pair when the object is first assigned. Not add the property in at a later statement.
var data = {q: "Portland,OR"};
If this doesn't help you can you post a link to the challenge?
Cameron Mosser
7,102 PointsThe data type you provided is invalid, data.q = "Portland,OR"; will not suffice.
$(document).ready(function() {
var weatherAPI = 'http://api.openweathermap.org/data/2.5/weather';
var data = { q: "Portland, OR"
};
});
Jim McQuarrie
10,597 PointsThe answer is
var data = {
q: "Portland, OR"
};
Charles Franklin
17,535 PointsI know this is late in coming but I was just struggling with this one. I have a hard time remembering when to put a semicolon in. I came up with Jim's answer on my own but I put a semicolon in after "Portland, OR" like this
"Portland, OR"; };
It didnt like the semicolon after OR.. I struggle with knowing when to put semicolons in and not...
Sandeep Krishnan
9,730 Pointsi have same issue with Javascript - I am always confused where to put semicolons and where to skip
Eric Conklin
8,350 PointsEric Conklin
8,350 PointsHi Jason. Not sure how I would link to it. It is the AJAX API challenge in the AJAX section of this course with the title I put above.
I will try using that code, however, it asks to define an empty object, THEN define a property within that object, so I don't know if it will work. Anyways, I am just frustrated as I'm using the same syntax as on W3 schools and many other tools and no luck for me :-(
Jason Anello
Courses Plus Student 94,610 PointsJason Anello
Courses Plus Student 94,610 PointsOk, I tracked down the code challenge. http://teamtreehouse.com/library/set-up-the-data-to-pass-to-the-api
I verified that the answer I gave is correct. The code you wrote and the code I gave in my answer should ultimately end up doing the same thing. I suspect that the challenge simply wants you to do it inside the curly braces and not afterwards.
Since the example in the instructions shows the colon syntax it's hinting that it should go in the curly braces. I'm not aware of a way to use colon syntax outside the curly braces.
One way to link to the challenge is to simply copy the url that's in your address bar and then paste it in like I did at the beginning of my comment here.
A better way is to use the "ask a question" button that you'll find on video, quiz, and code challenge pages. If you ask your question through that interface then it will be automatically linked to your question.