I am a beginner at JavaScript, and am attempting to learn by diving in and building something. I may be in over my head trying to use JSONP on my first project, but I am trying to create a script that will use the LaunchLibrary API to display upcoming rocket launches.
Specifically, I am calling this JSONP object.
Up to now, my code only attempts to access certain points of the data for each launch, and store them in an array so that I can access them later when I want to display the data.
function launchDisplay(data){
//Gather and store relevant data
var rocketName = [];
for(i = 0; i < data.launches.length; i++){
rocketName.push(data.launches[i].rocket.name);
}
console.log(rocketName);
}
However, this code generates an
Uncaught SyntaxError: Unexpected token :
in the console on what appears to be the first key:value pair. The JSON object is deeply nested, and starts with a "total":10, which is the number of launches returned by my search criteria. The second item in the object is where my data is, a launches array, which contains all of the launches returned by the search. There are other objects nested inside of the launches object, which I need to access.
Any help on what I am doing wrong?
EDIT: The code now is working. It appears the API I was using needed JSON, not JSONP. Also, the functions were running synchronous, not asynchronous. Here is the working JSFiddle, for reference.